DEV Community

KARTHIKEYAN M
KARTHIKEYAN M

Posted on

Day 2 of Java Mastery: Understanding Different main Method Syntaxes in Java for Beginners

🔰 Starting out in Java?
You’ve probably seen this line over and over:

public static void main(String[] args) {
// your code here
}

But did you know Java allows several valid variations of the main method?

Let’s break it down for clarity! 👇

✅ 𝑽𝒂𝒍𝒊𝒅 𝒎𝒂𝒊𝒏 𝑴𝒆𝒕𝒉𝒐𝒅 𝑺𝒚𝒏𝒕𝒂𝒙𝒆𝒔:

1️⃣ public static void main(String[] args)
→ Standard & most widely used

2️⃣ public static void main(String args[])
→ Old-school array syntax (still valid)

3️⃣ public static void main(String... args)
→ Uses varargs — flexible and works the same

4️⃣ public static void main(String[] myCustomName)
→ Parameter name can be anything!

❌ 𝙄𝙣𝙫𝙖𝙡𝙞𝙙 𝙎𝙮𝙣𝙩𝙖𝙭𝙚𝙨:
🚫 public void main(String[] args) → Missing static
🚫 static void main(String[] args) → Missing public
🚫 public static void main(int[] args) → Wrong parameter type

🔎 The JVM specifically looks for:
public static void main(String[] args)

🧠 𝙁𝙪𝙣 𝙁𝙖𝙘𝙩:

You can overload the main method, but only the correct one (String[] args) will run by default!

📚 𝗡𝗲𝘄 𝘁𝗼 𝗝𝗮𝘃𝗮?

Check out my full beginner-friendly blog post on this topic:
👉 https://wp.me/paNbWh-2l

💬 Got any Java tricks you wish you knew earlier? Drop them below 👇
Let’s grow together.

Java #100DaysOfCode #FullStackDevelopment #CodingJourney #Beginners

Top comments (0)