🔹 Java main() Method Syntax – Explained in Simple Words
"An investment in knowledge pays the best interest." — Benjamin Franklin
In this guide, we’ll explore different ways the main() method can be written in Java. You might see various syntaxes — some valid, some invalid, and some that look okay but are not suitable as the main method. Let's simplify it all here.
✅ What is the main() Method?
In Java, the main() method is the starting point of every program. It tells the Java Virtual Machine (JVM) where to begin executing your code.
The standard syntax looks like this:
public static void main(String[] args)
Let’s break it down:
| Keyword | Meaning |
|---|---|
public |
Accessible from anywhere |
static |
No object is needed to call it |
void |
It doesn’t return any value |
main |
The method name recognized by JVM to start execution |
String[] args |
Accepts command-line arguments in the form of a string array |
✅ Different Valid and Invalid main() Method Syntaxes
Let’s go one-by-one with easy explanations for each variation you might encounter.
✅ Valid main() Method Variations
All of these are allowed in Java, and your program will run successfully:
public static void main(String[] args)
static public void main(String[] args)
✔ Changing the order of
publicandstaticis allowed.
public static void main(String args[])
✔ You can place
[]after the variable instead of after the type.
public static void main(String []args)
public static void main(String[ ] args)```
```java
public static void main(String[] xyz)
✔ You can change the name
argsto anything — it's just a variable name.
public static void main(String... args)
✔ This is called a varargs version. It works the same as
String[] args. But remember:
Only 3 dots (...) are allowed, no more or less.
final public static void main(String[] args)
synchronized public static void main(String[] args)
strictfp public static void main(String[] args)
✔ All above are legal — adding modifiers like
final,synchronized, orstrictfpis allowed.
❌ Incorrect or Invalid main() Methods
These examples may look fine but will not work as the main() method — either they are wrong for JVM or just wrong syntax altogether.
public void static main(String[] args)
❌ Invalid: You must write returnType (void) before the method name.
🛑 JVM will throw a compile-time error: “Method name expected after return type”
public static void args(String[] args)
public static void xyz(String[] args)
❌ Invalid as main method:
✔ These are valid custom methods, but JVM will not consider them as the starting point because the method name is not main.
public static void main(Integer[] args)
❌ Invalid as main method:
✔ This is valid method syntax but JVM requires String[] as argument. Not Integer[].
protected static void main(String[] args)
private static void main(String[] args)
❌ Invalid as main method:
✔ These are valid Java methods, but the JVM needs the method to be public to access it from outside the class.
public static void main(String args() {})
❌ Invalid: Wrong use of syntax. args() is treated as a method or function.
public static void main(String args)
❌ Invalid for main():
✔ Valid Java method, but again, JVM expects a String[] argument, not just a String.
public void main(String[] args)
static void main(String[] args)
❌ Invalid as main() method:
✔ JVM requires main() to be both public and static. These will compile but will throw runtime error:
Main method not found in class Test...
🧠Recap Table: Summary of Validity
| Syntax Variation | Valid main()? |
Notes |
|---|---|---|
public static void main(String[] args) |
✅ | Standard main method |
static public void main(String[] args) |
✅ | Order doesn't matter |
public static void main(String args[]) |
✅ | [] after variable is okay |
public static void main(String... args) |
✅ | Varargs supported |
public static void args(String[] args) |
❌ | Not named main() |
public void static main(String[] args) |
❌ | Wrong order |
private static void main(String[] args) |
❌ | Must be public |
public static void main(Integer[] args) |
❌ | Wrong parameter type |
public static void main(String args) |
❌ | Needs to be an array |
public static void main(String args() {}) |
❌ | Totally invalid syntax |
public void main(String[] args) |
❌ | Not static |
static void main(String[] args) |
❌ | Not public |
🔚 Final Thoughts
-
The main method must follow strict syntax for the JVM to recognize and execute it.
-
You can tweak some parts like the argument variable name, use of
[], or keyword order. -
Avoid incorrect modifiers or return types that prevent execution.
📘 Pro Tip: Always use the standard form for interviews and real-world development:
public static void main(String[] args)
Comments
Post a Comment