Is a main method compulsory in all Java programs? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Is a main method compulsory in all Java programs?

In the second question of the module quiz of basic Java, all the three options are wrong. However, the second option has been set as correct. Java need not have a main method!

11th Mar 2018, 10:34 AM
knave
knave - avatar
4 Answers
+ 14
yes //thats the starting point of java program , from where the compiler starts compiling. //if no main method , then there must be someOther method to define the starting pt.
11th Mar 2018, 10:58 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 2
main method is kinda important its the start of execution 😊
11th Mar 2018, 10:40 AM
D_Stark
D_Stark - avatar
+ 1
Yeah, I get your point, but main method is not compulsory as such. We can write programs without a main method and use them in some other Java programs or as a web backend.
11th Mar 2018, 10:45 AM
knave
knave - avatar
+ 1
If you don't write main method then you will get run time exception. Prior to jdk 5, you can write some code under static block and terminate the program right there. But after jdk 5, it is compulsory to write main method. Otherwise you will get run time exception saying main method not found
11th Mar 2018, 11:08 AM
Ankit Saxena
Ankit Saxena - avatar