๐ฐ 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.
Top comments (0)