Is main method compulsory in Java?

Hi,

Can we have a executable class in Java without a main method? I am going to write simple Java program and there is main method.

Is main method compulsory in Java?

Thanks

View Answers

May 19, 2017 at 1:52 PM

Hi,

Main method is import in Java program. This is the point from where JVM star running the program. So, it you want your class to be run by JVM then you should have a main method.

JVM looks for the main method in the compiled code and then starts the execution of the program.

This applicable for all the program that you directly executes using JVM. But in case of server application its not required, because JVM first runs the Server program and in turn server program runs your class files.

You can follow following examples:

In all the above examples you will find the use of main methods.

Thanks









Related Tutorials/Questions & Answers:
Advertisements