Sub topic 3
Instance Main Methods
Prerequisites
This chapter assumes that you know basic Java syntax, including classes, fields, methods, and constructors. You should also recognize the traditional entry point:
public static void main(String[] args)A Java program starts at an entry point, which is the method the Java launcher calls first. In older Java versions, the entry point had to be static and had to accept a String[] parameter. Java 25 also supports simpler instance entry points.
What an instance main method is
An instance method belongs to an object created from a class. It is different from a static method, which belongs to the class itself.
An instance main method can have no parameters:
class Greeter {
private int count;
void main() {
count++;
IO.println("Greeting number " + count);
}
}When you launch Greeter, Java performs these steps:
- It finds the
mainmethod. - It creates a
Greeterobject. - It calls that object's
mainmethod. - The method increments the object's
countfield and prints the result.
The field works without static because the method runs on a particular Greeter object.
How the launcher chooses a main method
Java looks for a suitable method named main. The method must return void. Depending on the available methods, the launcher can use a static or instance form.
| Method form | Meaning |
|---|---|
| `static void main(String[] args)` | Traditional class-level entry point |
| `void main(String[] args)` | Instance entry point with command-line arguments |
| `static void main()` | Static entry point without arguments |
| `void main()` | Instance entry point without arguments |
If a class contains several valid forms, Java uses a defined preference order. The traditional static method with a String[] parameter has priority. Less traditional forms are considered after it. Do not depend on two methods with the same parameter types but different return types, because Java does not allow that combination.
The launcher also checks whether an instance entry point can be used. The class must have an accessible no-argument constructor. Java calls that constructor before it calls main.
Why use an instance main method?
Instance main methods remove two pieces of ceremony:
- You do not need the
statickeyword. - You can omit the unused
String[] argsparameter.
They also make small programs easier to grow. Code in main can directly read and update instance fields. You do not need to make every field static merely because the entry point is static.
For example, this program keeps the greeting count as object state:
class Greeter {
private int count = 10;
void main() {
count++;
IO.println("Greeting number " + count);
}
}Running it prints:
Greeting number 11The class still has an ordinary class declaration. Compact source files remove even that declaration, but instance main methods can be used in a normal class first.
Common errors
- A method named
Mainis not an entry point. Java names are case-sensitive. - A method returning
intorStringis not a validmainmethod. - An instance main method fails if Java cannot access a no-argument constructor.
- A constructor that requires arguments cannot be used automatically by the launcher.
- A static field is not required just because the program starts from
main.
Exercise
Why can the main method in Greeter read and update count without declaring either one as static?
Show answer
The launcher creates a Greeter object first, then calls the instance method on that object. Both count and main belong to the same object.