Java Main Method

public static void main (String args[]) which is commonly known as a main() method in java, is the start point of the java program. In other words, we can say that it is the execution point for any java program, without the main() method JVM cannot run the java program.

The syntax of main method is given below.

Syntax of main method.

public : In java we use public access specifier for main method, so that the java runtime can execute this main method. In main method other access specifiers like private and protected are not used for main() method because it will not be visible to JVM and we wont be able to execute the java code. Therefore main() method in java should always have public access specifier.

static : There is no object of the present class when java runtime starts, therefore main method should be given as static so that Java Virtual Machine can load the class into the memory and call the main method. JVM cannot invoke the main method if we do not make main method static as no object of the class is present.

void : In java it is essential for every method to have a return type. As main() method do not return anything we use void as return type for main() method in Java.

main : This is the method name for java and by default we use main method because it is predefined in the JVM. It is called by JVM to execute a program line by line and end the execution after completion of this method. Moreover we can even overload the main() method.

Example of Main Method

Rutu Shah