Wednesday, 24 July 2013

Why should Main method in Java carry public static and void in its syntax always?

public static void main(String args[])

Every java programmer wondered to follow the syntax for main method as mentioned above. This is the reason behind the syntax. 

Why public - The access specifier of Main method is said to be public so that main method can be called outside of the class . This makes sure that JVM can easily access and execute the main method.

Why static - Main method is declared static so that JVM can call the main method with out creating instance for it. [While JVM tries to execute java program, it does not know how to create instance as there is no standard constructor defined for Main class].

Why void  - This is to make sure that Main method is not returning any values.

Cheers! Happy coding!


No comments:

Post a Comment