🧠 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.
Java uses access modifiers to control visibility. public
means:
“Accessible from anywhere, including outside the package.”
Since the JVM needs to invoke main()
from outside your class, it must be public. If it's private
, you’ll get a runtime error.
❌ Example (Incorrect):
private static void main(String[] args) {
System.out.println("This won't work!");
}
Error: Main method not found in class...
🔍 Why is main()
Method static?
✅ Reason:
Because the JVM doesn’t create an object of your class to run it.
Java’s static
keyword means:
“This method belongs to the class itself, not to an instance of the class.”
When the JVM starts your program, no objects exist yet. So main()
must be static so that the JVM can call it like this:
MyApp.main(args);
Without static
, you'd need to write:
new MyApp().main(args); // Which is not possible before object creation
That’s why main()
must be static.
🧾 Why void
Return Type?
Because the main()
method does not return any value to the JVM.
Its only job is to kick off your application logic. If you try to change it to return int
or any other type, the JVM will not recognize it as the entry point.
✅ Correct:
public static void main(String[] args)
❌ Incorrect:
public static int main(String[] args) { return 0; }
💡 Why String[] args
?
Because this is how command-line arguments are passed to your Java program.
When you run:
java MyApp Hello World
The args
array receives:
args[0] = "Hello"
args[1] = "World"
You can then process these arguments in your code if needed.
🔎 What Happens If You Change the Signature?

✅ Summary

📚 Final Thoughts
Java requires the main()
method to be in the exact format:
public static void main(String[] args)
Each keyword in this method has a specific purpose tied to how the JVM loads and executes your application.
Understanding this signature is essential for every Java developer. It may look like boilerplate, but it’s a well-designed contract between you and the Java runtime.
Comments
Post a Comment
Leave Comment