Member-only story
Why is main() Method Public Static in Java? (Here’s the Real Truth!)
Understand why Java requires the main() method to be public and static. Learn the reasoning behind the keywords and how Java programs start execution.
Introduction
Every Java programmer has written this line at some point:
public static void main(String[] args)
But have you ever wondered why it’s written this way? Why must it be public
? Why static
? What happens if we change it?
In this article, we’ll explain in simple terms why the Java main()
method must be public static
, and what each keyword means in the context of program execution.
🚀 What is the main()
Method in Java?
The main()
method is the entry point of any standalone Java application. It’s the method the JVM (Java Virtual Machine) looks for when you run a program.
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
Without this method, your program won’t run unless you’re using a framework like Spring Boot or JavaFX that defines its own entry point.
🔍 Why is main()
Method public?
✅ Reason:
Because the JVM must be able to access it from outside the class.