Sub topic 2
How the Launcher Chooses a Main Method
Assume you know basic Java methods, classes, and the traditional public static void main(String[] args) method. This chapter focuses on how the Java launcher selects a method when a class has more than one possible entry point.
A main method is the method where a launched Java program begins. In Java 25, the launcher accepts four useful forms:
| Priority | Method form | Object required? |
|---|---|---|
| 1 | `static void main(String[] args)` | No |
| 2 | `static void main()` | No |
| 3 | `void main(String[] args)` | Yes |
| 4 | `void main()` | Yes |
String... args is equivalent to String[] args. The method must return void. It cannot be private.
The selection order
The launcher checks candidates in the order shown in the table. It prefers a static method that accepts arguments over a static method with no parameters. If no static method exists, it looks for an instance method.
Consider this class:
public class LaunchChoice {
public static void main(String[] args) {
System.out.println("The String[] main method ran.");
}
public static void main() {
System.out.println("The no-argument main method ran.");
}
}Both methods are valid candidates. The launcher chooses main(String[] args) because it has the highest priority. Run it with:
java LaunchChoice.javaThe output is:
The String[] main method ran.The launcher supplies an empty array when you do not provide command-line arguments. If you run:
java LaunchChoice.java one twothen args contains "one" and "two".
A method with a different parameter type is not a candidate:
void main(String message) {
System.out.println(message);
}This method accepts one String, not a String[], so the launcher ignores it.
Instance main methods
An instance method belongs to an object rather than to the class itself. The launcher can use one when no higher-priority static method is available.
class Greeter {
private int count;
void main() {
count++;
System.out.println("Greeting number " + count);
}
}When you launch Greeter, Java performs these steps:
- It finds
void main(). - It creates a
Greeterobject. - It calls the object's
mainmethod.
The class must have an accessible no-argument constructor. If you do not write any constructor, Java supplies a default no-argument constructor.
Instance main methods can use fields directly because they run on an object:
class Counter {
private int value = 10;
void main() {
value += 5;
System.out.println(value);
}
}The output is 15.
Common errors
- Writing
main(String)instead ofmain(String[]). - Returning
intor another type instead ofvoid. - Declaring the only constructor with parameters, which prevents instance launching.
- Marking the method
private. - Expecting an instance main method to run without a constructible object.
Exercise
A class contains static void main(), void main(String[] args), and void main(). Which method does Java select?
Show answer
Java selects static void main() because it is the highest-priority available candidate.