Why Is Java’s main Method Public, Static, and Void? Explained
This article explains why the Java main method must be declared public, static, and void, covering its role as the program entry point, the JVM's requirements, and additional modifiers and variations such as varargs and strictfp.
Main method is the entry point of Java programs; the JVM looks for public static void main(String[] args) and throws NoSuchMethodError if it is missing.
It must follow the exact signature; since Java 1.5 varargs can be used, e.g., public static void main(String... args).
Why is main static?
1. Static allows the JVM to invoke it without creating an instance of the class.
2. It mirrors the C/C++ main method.
3. If not static, the JVM would need to instantiate the class, but overloaded constructors would make it ambiguous which method to call.
4. Static methods are loaded into memory and can be called directly.
Why is main public?
Public makes the method accessible from outside its class, enabling the JVM to call it.
Why does main have a void return type?
Returning a value from main has no meaning for program execution; void indicates no return value.
Summary
1. main must be declared public static void; otherwise the JVM cannot run the program.
2. If the JVM cannot find main, it throws NoSuchMethodError, e.g., running java HelloWorld searches for the required signature.
3. main is the program's entry point.
4. It runs on the special thread named "main" and continues until that thread ends or all non‑daemon threads terminate.
5. Exceptions like "Exception in thread main" indicate the error originated from the main thread.
6. Since Java 1.5 you can use varargs: public static void main(String... args).
7. Additional modifiers such as final, synchronized, and strictfp can be added, e.g.,
public strictfp final synchronized static void main(String[] args).
8. main can be overloaded, but the JVM only calls the signature defined above.
9. The method may declare a throws clause to propagate checked or unchecked exceptions.
10. Static initialization blocks execute before main when the class is loaded.
public static void main(String... args) public strictfp final synchronized static void main(String[] args)Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
