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:

or

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:

 

 

FAQs:

 

1. Why is main method declared static in Java?

The main method is static in Java so it can be directly invoked by the JVM without having to instantiate an object of the class.

If the main method is non-static, then JVM needs to create an instance of the class and there would be an ambiguity if constructor of that class takes an argument – which constructor should be called by JVM and what parameters should be passed? We know that JVM can’t instantiate a java class without calling a constructor method. We can agree upon a default constructor but that’s extra overhead. Also, the class must not be abstract otherwise the JVM could not instantiate it. So to make Java little less verbose than it already is, the main method is static in Java.

 

2. Why main method is public in Java?

We know that anyone can access/invoke a method having public access specifier. The main method is public in java because it has to be invoked by the JVM. So if main() is not public in Java, the JVM won’t be able to call it.

 

3. Java program execution 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.

 

4. 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.

 

5. Can we Overload/override main method?

The main() can be overloaded in Java. We can have any number of main methods defined in a Java class having different method signatures but the class should contain one main method with exact signature as “public static void main(String[] args)” else program will compile succesfully but throws below runtime error upon execution –

The program compiled successfully, but main class was not found.
Main class should contain method: public static void main (String[] args).

Example of running code:

Download   Run Code

Output:

Inside main
Inside overloaded main: 7
Inside overloaded main: 3.14

 
Please note that main() cannot be overloaded based on its return type. Unlike method overloading, we cannot override the main() in Java since it is static and static methods cannot be overrided in Java since method overriding only occurs in the context of dynamic lookup of methods and static methods are looked up statically at compile-time itself.

 
Thanks for reading.




Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 





Leave a Reply

Notify of
avatar
wpDiscuz