Why is the main method public static void in Java?
The JVM starts a program by calling public static void main(String[] args).
- public: the JVM is outside your class, so the method must be visible.
- static: the JVM calls it before any object exists.
- void: it does not return a value to the JVM.
- String[] args: the command line arguments.
Since Java 25 you can write a short form for small programs, with no class and no public static:
void main() {
IO.println("Hello from a compact source file");
}
Output
Hello from a compact source file
Learn more about the similar topics:
| Tutorials |
|---|
| Java Introduction |
| What is Java? JDK, JRE and JVM |
| Java Install and Setup |
| Your First Java Program: Hello World |
| Java Syntax |
| Java Comments |
| Java Cheat Sheet |
| Exercises & Assignments |
|---|
| No Content Found. |