Quick revision
The short answer
In Java 25 the launcher looks for a non-private main method, declared in the class or inherited, and prefers main(String[]) over a main() with no parameters. If the chosen method is static it is called directly; if it is an instance method, the launcher creates an object through a non-private no-argument constructor and calls main on it, and launch fails if there is no such constructor. Existing classes with public static void main(String[] args) run exactly as before.
What to remember
In this answer, we saw how the Java 25 launcher chooses an entry point: it considers non-private main methods, declared or inherited, and prefers main(String[]) over main(). A static choice is called directly, while an instance choice needs a non-private no-argument constructor so the launcher can create the object first, and without one the launch fails. Existing public static void main methods behave exactly as before, the visible changes are at the edges and in tools that detect main classes, and for production services the classic static main remains the clearest option.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
In Java 25 the launcher looks for a non-private main method, declared in the class or inherited, and prefers main(String[]) over a main() with no parameters. If the chosen method is static it is called directly; if it is an instance method, the launcher creates an object through a non-private no-argument constructor and calls main on it, and launch fails if there is no such constructor. Existing classes with public static void main(String[] args) run exactly as before.
Why this question is asked
Before Java 25, the launcher had one rule: find public static void main(String[] args) or fail. JEP 512 relaxed that rule, so main can now be an instance method, can take no parameters and does not have to be public. Once there are several possible shapes, a class may contain more than one candidate, and we need to know which one runs. Interviewers ask this to check whether we understand how the java command really starts a class and whether we can judge the effect on existing code and tools. In this answer, we will go through the selection rules, see how an instance main is invoked, try the tricky cases in code, and then discuss what changes, and what does not, for existing applications.
1A Quick Recap of What the Launcher Does
When we type java com.acme.Tool, the launcher, the small piece of the JDK behind the java command, loads the class com.acme.Tool, initializes it, finds an entry point and calls it with the command-line arguments. Class initialization means running its static initializers, such as static fields with initial values. Until Java 25, the entry point had to be exactly public static void main(String[] args), and the launcher printed an error asking for that signature if it was missing.
Two terms matter below. A static method belongs to the class and is called without an object. An instance method belongs to an object, so somebody must create the object first. Inherited methods are those declared in a superclass that the class gets without redeclaring them.
2How the Launcher Chooses Among Candidates
In Java 25, a candidate is a method called main that is not private, returns void, and has either a single String[] parameter or no parameters. It may be static or instance, and public, protected or package-private, and it may be declared in the class itself or inherited from a superclass. The launcher first looks for main(String[]); only if there is none does it look for main(). A class cannot have a static and an instance main with the same parameters, because they would have the same signature, so after this rule there is never more than one method left.
- Load and initialize Toolstatic initializers run first
- Non-private main(String[]) declared or inherited?YesChoose itNoLook for main()
- Non-private main() declared or inherited?YesChoose itNoFail: no main method
- Is the chosen method static?YesCall it directlyNoCreate an instance first
- Non-private no-arg constructor?Yesnew Tool(), then call mainNoFail: cannot instantiate
5 steps in order. Play them, or step through them one at a time.
main(String[]) always beats main(); instance methods need a usable no-arg constructor.
Once the method is chosen, the second decision is how to call it. A static method is called directly. An instance method needs an object, so the launcher looks for a non-private constructor with no parameters, creates an instance with it, and then calls main on that instance. If the class has only constructors with parameters, or only a private one, the launch fails with an error, even though a main method exists.
3Trying the Tricky Cases
The rules are easiest to remember from small examples. Each class below is launched on its own with java and the class name.
Notice the first case: even though main() is an instance method that looks newer, the String[] version wins because it is checked first, and being static has nothing to do with it. In the second case the private method is invisible to the launcher, so the instance main() is used. In the third case a main method exists but cannot be called, because there is no way to create the object. The last case shows that inherited methods count: the launcher creates a Child, so getClass() reports Child, and calls the main it inherited from Base.
4What This Means for Existing Code
For a class that already has public static void main(String[] args), nothing changes. It is a non-private main(String[]), so it is found first, it is static, so it is called directly, and the behaviour is byte for byte what it was. Every existing service, batch job and Spring Boot application keeps starting the same way.
The changes are at the edges. A class whose only main was package-private or protected used to fail to launch and now runs. A class that happens to have an unrelated instance method called main() with no parameters, perhaps a helper in a test, can now be launched by accident. And a class with both a static main(String[]) and some other main is unaffected because the String[] version still wins.
Tools are the area to watch. IDE run configurations, the Maven and Gradle plugins that detect main classes, Spring Boot's build plugins that pick a Start-Class, and code scanners all used to look for the one classic signature. Older versions may not offer a run button for an instance main, or may not detect it as the main class. Check that your IDE and plugin versions support Java 25 entry points before relying on them.
5What We Should Still Use in Production
For production applications, the classic public static void main(String[] args) remains the clearest choice. Every developer, tool and runbook recognises it, it receives the arguments that real programs nearly always need, and it does not depend on a constructor running before main. The new forms shine in compact source files, teaching and small utilities. A team guideline can simply say: services keep the classic static main; scripts and examples may use the short form.
6Follow-up questions
Can a superclass's main be launched through a subclass?
Yes. Inherited methods are candidates, so java Child runs a main declared in Base. If that main is static, it runs without any object, much as it did before Java 25, although getClass() is not available in a static method. If it is an instance main, the launcher creates a Child, not a Base, so overridden methods and getClass() reflect Child. If Child is abstract, the launcher cannot create it and the launch fails.
Is the class initialized before the launcher decides how to call main?
Yes. The class is loaded and initialized first, so static initializers run even if the launch then fails because no constructor is usable. For an instance main, instance field initializers and the constructor run next, and only then main. That order matters when initialization has side effects, such as reading configuration.
Do the arguments get lost with main()?
The launcher simply does not pass them, because main() has no parameter. If the class also declared main(String[]), that one would have been chosen. A program that needs arguments should declare main(String[] args), which works as an instance method too.
7Conclusion
In this answer, we saw how the Java 25 launcher chooses an entry point: it considers non-private main methods, declared or inherited, and prefers main(String[]) over main(). A static choice is called directly, while an instance choice needs a non-private no-argument constructor so the launcher can create the object first, and without one the launch fails. Existing public static void main methods behave exactly as before, the visible changes are at the edges and in tools that detect main classes, and for production services the classic static main remains the clearest option.
Open with
Since JEP 512 in Java 25, a main method can be static or instance, can take String[] or nothing, and only has to be non-private. The launcher first loads and initializes the class.
Then, if they want more
Then it looks for a non-private main with a String array parameter, including inherited ones, and only if there is none does it look for a main with no parameters. A class cannot have a static and an instance main with the same parameter list, so that always leaves at most one method.
If the chosen method is static, the launcher calls it directly. If it is an instance method, the launcher needs a non-private no-argument constructor; it creates the object and calls main on it, and if no such constructor exists, the launch fails.
Inherited methods count, so launching a subclass can run a main declared in its parent, on a subclass instance. For existing code, public static void main of String array is still found first and called the same way, so nothing changes for real services.
The differences are at the edges: package-private mains now launch, and tools such as IDE run configurations or build plugins that detect main classes need versions that understand the new forms. For production code I would still write the classic static main.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 25
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "With instance main methods allowed in Java 25, how does the launcher choose which main...": https://scrivio-interview-library.netlify.app/java/classes-and-objects/faa2c9aac8/