Sub topic 3
Instance main methods
This chapter assumes you know the basic syntax of Java (variables, methods, control flow) and what a class, field, and method are. It also assumes you can compile and run a program from the command line and that you know the traditional public static void main(String[] args) method. Instance main methods are final in Java 25. Earlier releases (Java 21 to 24) required preview flags.
What changes
The traditional entry point asks a beginner to understand public, static, void, String[], and a class declaration before any output appears.
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}Java 25 relaxes the rules for how the java launcher finds the entry point. Under the new rules, the entry point can:
- be non-public,
- be an instance method instead of a static one,
- omit the
String[] argsparameter.
The launcher still needs a method named main that returns void. It just accepts more shapes of it.
An instance main method
An instance method belongs to an object, not to the class. To call one, the launcher must first create an object. Here is a class whose entry point is an instance method:
class Greeter {
private int count;
void main() {
count++;
IO.println("Greeting number " + count);
}
}The class is not public. The method is not public or static, and it takes no parameters. Running it prints Greeting number 1.
The launcher does these steps:
- It loads
Greeter. - It calls the accessible no-argument constructor to create an instance.
- It calls
main()on that instance.
The instance is new on every run, so count always starts at 0 and the output is always Greeting number 1. Instance main is useful because the entry point can use fields and other instance methods directly, without a static context.
IO.println is a static method of java.lang.IO. Because java.lang is always imported, you need no import. A later chapter covers IO in detail.
How the launcher selects a method
A class can declare several methods that look like entry points. The launcher checks for candidates in a fixed order and uses the first one it finds.
| Priority | Shape | Where it can 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 |
All candidates must have non-private access: public, protected, or package-private. Static methods rank above instance methods. Within each group, the version with String[] args ranks above the one without.
The next program has two candidates. It shows that the static one wins even though it ranks by a different rule than the instance one:
class Priority {
static void main() {
IO.println("static, no parameters");
}
void main(String[] args) {
IO.println("instance, with parameters");
}
}Run with java Priority.java, it prints static, no parameters. A static method always beats an instance method, whatever their parameter lists are. If you remove the static method, the instance method runs. If you also remove its parameter, void main() runs.
You cannot declare both static void main() and void main() in one class. They have the same signature, so the compiler rejects the duplicate.
Rules an instance main must follow
For an instance main method to launch, these conditions must hold:
- The method is named
main, returnsvoid, and is notprivate. - It takes either no parameters or one
String[]parameter. A varargsString...parameter also works because it is aString[]in the signature. - The launched class has a non-private constructor with no parameters. Either declare one or let the compiler supply the default constructor.
- The launched class is not abstract.
The default constructor of a class declared without any constructor has the same access as the class. For a package-private class, that is package-private, which is accessible to the launcher.
An instance main method can also be inherited:
class Base {
void main() {
IO.println("Hello from " + getClass().getSimpleName());
}
}
class Derived extends Base {
}Launching Derived finds main() in Base, creates a Derived instance, and prints Hello from Derived. With the source launcher, put both classes in one file such as Derived.java, and make sure the class you want to launch is the first one in the file. For java Derived.java, put Derived first and Base second. You can also compile with javac Derived.java and run java Derived.
class Derived extends Base {
}
class Base {
void main() {
IO.println("Hello from " + getClass().getSimpleName());
}
}Instance state and lifecycle
Because the launcher creates the object, constructors and field initializers run before main:
class Setup {
private final String label;
Setup() {
label = "ready";
IO.println("constructor runs first");
}
void main() {
IO.println("main sees: " + label);
}
}The output is:
constructor runs first
main sees: readyAnything that throws in the constructor stops the launch before main runs. The launcher reports the exception from the constructor, not from main.
If you need args, declare the one-parameter form:
class Echo {
void main(String[] args) {
for (String arg : args) {
IO.println(arg);
}
}
}java Echo.java one two prints one and two on separate lines.
Common errors
Most failures come from a method that does not qualify as a candidate. The launcher then reports that it found no suitable main method.
| Problem | Why it fails | Fix |
|---|---|---|
| `private void main()` | Private methods are not candidates | Remove `private` |
| `int main()` | The return type must be `void` | Change it to `void` |
| `void main(int x)` | The parameter list is not empty or `String[]` | Use `String[]` or no parameters |
| Constructor `Greeter(String name)` only | No no-argument constructor is available | Add `Greeter() {}` |
| `private Greeter()` | The launcher cannot call a private constructor | Make it non-private |
| `abstract class Greeter` | The launcher cannot create an instance | Make the class concrete |
Two further points cause confusion:
- The launcher takes the first class in a source file. With
java File.java, the first top-level class declared in the file is launched, regardless of the file name. Helper classes placed first will be launched instead and may have no main method. - A static method hides nothing. If you add a leftover
static void main(String[] args)to a class with an instance main, the static one runs and the instance one is ignored without any warning.
Compatibility with existing programs
Every program written with public static void main(String[] args) still works, and it ranks first in the selection order. Libraries and frameworks that call main by reflection are not changed by this feature. Tools that scan class files for a public static main method will not discover instance main methods, so check your build tooling (for example, an executable JAR manifest or a plugin that finds the main class) before relying on the new forms. Setting Main-Class explicitly in a manifest is unaffected, and the JVM applies the same selection when launching it.
Where this leads
Instance main removes static, public, and String[] args from the first program. A class declaration is still required. The next chapters remove that too: a compact source file contains only methods and fields, and the compiler wraps them in a class for you. The launcher rules in this chapter apply unchanged to that generated class.
Exercise
A class declares static void main(String[] args), static void main(), and void main(). Which one does the launcher call, and which one would it call if you deleted the first two?
Show answer
It calls static void main(String[] args), because static methods with a String[] parameter have the highest priority. If you delete the first two, the launcher creates an instance of the class and calls void main(), provided the class has a non-private no-argument constructor.
Exercise
This file fails to launch with java Report.java. Give two separate reasons it could fail, and show a fix for each.
class Report {
private String title;
Report(String title) {
this.title = title;
}
private void main() {
IO.println(title);
}
}
Show answer
First, main is private, so it is not a candidate. Change it to void main(). Second, the only constructor takes a parameter, so the launcher cannot create an instance. Add a no-argument constructor, for example Report() { this("untitled"); }. After both fixes the program prints untitled.