📘 Understanding public static void main in Java – Explained for Beginners

1. Introduction
Welcome back to The Learning Loop! This is the second post in my Java learning series. If you’re just joining in, you might want to start with:
📌 Getting Started with Java – A Beginner's Guide
📚 Back to Java Learning Path
In the last post, we wrote our very first Java program the famous “Hello, World!”. But there was one strange line in that program that we typed without really understanding it:
javaCopyEditpublic static void main(String[] args)
Today, let’s break it down piece by piece so it actually makes sense.
2. Why public static void main
is Important
Every Java application starts with one thing: a main method.
When you run a Java program, the Java Virtual Machine (JVM) looks for the main
method as the entry point to start executing your code.
If the JVM can’t find it, your program won’t run you’ll get an error like:
javascriptCopyEditError: Main method not found in class...
So, without public static void main
, Java doesn’t know where to start.
3. Breaking It Down
Let’s revisit our example from the first post:
javaCopyEditpublic class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
Now, let’s understand each word:
public
Means anyone can access it from anywhere in the program.
Since
main
is the starting point, it must be accessible to the JVM, so it has to bepublic
.
static
Means the method belongs to the class itself, not an object.
Without
static
, we’d need to create an object of the class before runningmain
which would be impossible, because we needmain
to even start running code.
void
Means the method doesn’t return any value.
main
just runs your code; it doesn’t send anything back to the JVM.
main
This is the exact method name the JVM looks for to start your program.
If you change it, your program won’t run unless you tell the JVM otherwise (which is advanced and rare).
String[] args
This means
main
can accept command-line arguments.For example, if you run:
nginxCopyEditjava HelloWorld Hello Java
args[0]
will be"Hello"
andargs[1]
will be"Java"
.
4. Simple Example with Args
javaCopyEditpublic class Greetings {
public static void main(String[] args) {
if(args.length > 0) {
System.out.println("Hello, " + args[0] + "!");
} else {
System.out.println("Hello, World!");
}
}
}
How it works:
If you run:
nginxCopyEditjava Greetings Janushan
Output:
CopyEditHello, Janushan!
If you run without any args:
CopyEditHello, World!
5. Common Beginner Questions
Q: Do I always need to write public static void main
?
Yes, if you want your program to run on its own. Later, when we get into GUI apps or frameworks, this changes — but for console programs, yes.
Q: Can I remove static
?
No — without static
, Java would need to create an object first, but it doesn’t know how yet.
Q: Can I change the name main
?
Not for the entry point — the JVM specifically looks for main
.
6. Final Thoughts
The public static void main
method is like your program’s “front door.”
It’s the place where Java steps inside your code and starts reading instructions.
Now that we know how the JVM finds and runs our code, we’re ready for the next step: Variables and Data Types — where we actually start storing and working with information in our programs.
7. Navigation
📌 Previous Post: Getting Started with Java – A Beginner's Guide
📚 Back to Java Learning Path
⏭ Next Post: Variables and Data Types in Java (coming soon)
Subscribe to my newsletter
Read articles from Janushan Alakendran directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
