Main method in Java

In Java, every application must contain a main method which serves as an entry point for the application similar to the main function in C and C++. The JVM (Java Virtual Machine) starts its execution by invoking the main method of the specified class and main() will subsequently invoke all the other methods required by the program.

The main method must be declared public, static and void. The public and static modifier can be either written as public static or static public, but the convention is to use public static. It accepts a single argument that is an array of strings through which runtime engine (JVM) passes information to the application via command-line argument. The main() can be written as:


The string array argument can be renamed to anything, but convention is to choose between “args” or “argv”. We can also make the main method final in Java by using final modifier and synchronized by using synchronized modifier in the method signature as shown below:




Is Java program execution possible without main method?

We have seen that the main method is serves as entry point for java applications. In Java 6 and before, it was possible to execute a java program without main by using static initializer. But starting Java 7, this won’t work.

Also for managed environments (container environments) like Servlet (or EJB), the main() is not the entry point of the application as they have their own life-cycle methods like init(), service() or destory() which is used by the container.


Difference between C/C++ and Java main method

In C/C++, the main() function serves as an entry point for program execution like Java but main() function in C/C++ is little different from main() in Java with respect to its method signature.

As evident from above method signatures, the main method in java does not return a value since it has return type void while the main() function in C/C++ returns an integer since it has return type int. Also, the main method in C/C++ is non-static unlike Java.

Why is main method declared public and static in Java?

Can we Overload/override main method?

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)


Thanks for reading.

Please use our online compiler to post code in comments. To contribute, get in touch with us.
Like us? Please spread the word and help us grow. Happy coding 🙂

Leave a Reply

newest oldest most voted
Notify of

this was superb