Sub topic 2
How the Launcher Chooses a Main Method
This chapter assumes you know basic Java syntax, what a class, field, and method are, and how to compile and run a program from the command line. It also assumes you know the traditional public static void main(String[] args) method. Examples need Java 25 or later, where instance main methods and java.lang.IO are final features.
The old rule and the new rule
Before Java 21, the launcher accepted exactly one shape: public static void main(String[] args). The JVM looked it up by name and descriptor, and anything else produced an error.
Today the launcher is more flexible. It scans the launched class for a method named main and picks the best candidate from a ranked list. The change affects only how the program starts. A main method is still an ordinary method.
What makes a method a candidate
A method is a candidate if it meets all of these conditions:
- Its name is
main. - Its return type is
void. - It is not
private. Public, protected, and package-private all qualify. - It takes either one
String[]parameter or no parameters.
Two things are no longer required: public and static. A method that fails any condition is ignored. For example, int main() is never launched, and neither is main(String[] args, int n).
The selection order
When more than one candidate exists, the launcher picks the first match in this order:
| Priority | Shape | Where it may be declared |
|---|---|---|
| 1 | `static void main(String[] args)` | The launched class |
| 2 | `static void main()` | The launched class |
| 3 | `void main(String[] args)` (instance) | The launched class or a superclass |
| 4 | `void main()` (instance) | The launched class or a superclass |
Static methods always beat instance methods, and a method that takes args beats one that does not. Static candidates must be declared in the launched class itself. Instance candidates can also be inherited.
Seeing the order in action
Static and instance methods cannot share the same parameter list, because Java does not allow two methods with the same signature. A class can still hold one of each shape by varying the parameters.
class Pick {
void main(String[] args) {
IO.println("instance main with args");
}
static void main() {
IO.println("static main without args");
}
}Save this as Pick.java and run java Pick.java. It prints static main without args. The instance method takes args, but the static method has higher priority. The launcher never creates a Pick object here.
Neither method is public. In earlier versions this class would fail to launch. Now it is fine, as long as the methods are not private.
Instance main and the constructor requirement
When the winning method is an instance method, the launcher must create an object before calling it. It uses a non-private constructor that takes no arguments. If the class declares no constructors, the compiler supplies a default one, which qualifies.
class Counter {
private int count;
void main() {
count++;
IO.println("Run number " + count);
}
}This prints Run number 1. Each launch creates a fresh Counter, so count starts at zero every time. Instance state lives only for that one run.
The next class cannot be launched, because the only constructor needs an argument:
class NeedsArg {
private final String name;
NeedsArg(String name) {
this.name = name;
}
void main() {
IO.println(name);
}
}The launcher has no value to pass for name, so it reports an error instead of calling main. Adding a no-argument constructor fixes it. A private no-argument constructor also fails, because the launcher requires a non-private one.
Inherited main methods
Only instance main methods can be inherited. Declare the method in a superclass and the subclass launches through it:
class Child extends Base {
}
class Base {
void main() {
IO.println("Running as " + getClass().getSimpleName());
}
}Run java Child.java. The source launcher starts the first top-level class in the file, so Child must come first. The output is Running as Child. The launcher instantiates Child, finds main through inheritance, and calls it on that instance. This is why getClass() reports Child.
A static main inherited from a superclass is not found. If Base declared static void main() instead, launching Child would fail. The launcher looks for static methods only in the launched class.
Access and the `String[]` parameter
Access modifiers affect selection only through the private rule. A package-private static void main(String[] args) is a valid priority-1 candidate. A private static void main(String[] args) is not a candidate at all, so the launcher moves on to the next shape.
The String[] parameter holds the command-line arguments after the class name or file name. Here is a program that uses them:
void main(String[] args) {
IO.println("Received " + args.length + " arguments");
for (String arg : args) {
IO.println("- " + arg);
}
}Running java Args.java one two prints Received 2 arguments, then the two arguments. If your program never reads its arguments, the no-parameter form is clearer. The no-parameter form loses nothing except access to args.
A varargs signature, main(String... args), is the same type as String[] and is treated as the priority-1 or priority-3 shape.
Common failures and what they mean
| Symptom | Likely cause |
|---|---|
| Launcher reports no main method found | The method is `private`, returns something other than `void`, has the wrong parameters, or is misspelled. |
| Launcher cannot instantiate the class | An instance `main` was chosen, but there is no accessible no-argument constructor. |
| Static `main` in a superclass is ignored | Static candidates must be declared in the launched class. |
| The "wrong" `main` runs | Overloads exist, and a higher-priority shape won. Check the table. |
| Field values reset on every run | The launcher creates a new instance each time. |
The "wrong main runs" case happens when you leave an old static void main(String[] args) in a class while editing a new instance main(). The old static method has priority 1, so it keeps running and your edit seems to have no effect. Delete the stale method.
Why a ranked order
The order keeps old programs working. Every program written for the traditional rule has a priority-1 method, so it behaves exactly as before. The new shapes only matter when no priority-1 method exists, or when you remove it.
The order also prefers the simpler model first. A static method needs no object, so the launcher avoids calling a constructor whenever a static candidate exists. Constructors run only when the program actually needs an instance.
Exercise
A class declares void main() (instance, package-private) and static void main(String[] args) (package-private). Which method does the launcher call, and does it create an instance of the class?
Show answer
It calls static void main(String[] args), which has priority 1. The launcher does not create an instance, because a static method needs no object. The instance main() is never called.
Exercise
You launch Child, which extends Base and declares no methods. Base declares static void main(). Does the launch succeed? What changes if you make main in Base an instance method?
Show answer
The launch fails, because static candidates must be declared in the launched class and Child declares none. If main becomes an instance method, the launch succeeds. The launcher creates a Child through its default constructor and calls the inherited main() on it, provided Child has a non-private no-argument constructor.
Exercise
A class has only public int main() and a no-argument constructor. Why does launching it fail?
Show answer
The return type is int, not void, so the method is not a candidate. With no other main method, the launcher finds nothing to call.