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)