Can we create a program without main method in java if yes then how? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can we create a program without main method in java if yes then how?

17th Dec 2016, 6:22 PM
Milan Singh
Milan Singh - avatar
9 Answers
+ 3
No, Main is the entry point of a program.
17th Dec 2016, 6:30 PM
Sheemin
Sheemin - avatar
+ 2
Every time you start your program, it looks for the Main function and then it continues to the rest of your code. You will always need some sort of a Main function.
17th Dec 2016, 9:07 PM
Michielvk
+ 1
ya we can with the help of static block... simple program-exit code if we write in static block.look it compile nd run nd exit the program.... without main..
18th Dec 2016, 11:26 AM
Milan Singh
Milan Singh - avatar
+ 1
hi😊 but by any way you never can write a program in java without main why! let me till you when you press run to run your code compiler the first thing he deals with is the main , the main is static method because the compiler can not create object from class from him self ,becuse he can not write any code ,he is just a software do some orders , so he call the main in the back ground with the name of class dot the name of method then take to start execute your program so by easy way we can say that main is the president and guied and compiler is the followers without the president all followers can not work I hope that is become clear 😊
21st Dec 2016, 4:44 AM
aya ahmed
aya ahmed - avatar
+ 1
static{ system.exit(0); } simple...A Java Class Excute Without A Main Method..Every Time not gud To Be A Theory Man.Be practical...its A Interview famous question..nd its answer is Yes..we Can excute a java Class without a main method just bcz Of static Block.
21st Dec 2016, 6:58 AM
Milan Singh
Milan Singh - avatar
0
just because JVM first call static ...
18th Dec 2016, 11:28 AM
Milan Singh
Milan Singh - avatar
0
A java program always looks to run the Main method first. If you want your starting method to have a name you choose, for whatever reason, then you can have main call your named method and do nothing else. at the start of the program. e.g: public static void main (String[] args) { doSomething(); }
19th Dec 2016, 2:35 PM
Will
Will - avatar
0
in the java 6 may be you can run your program without main using static block but not in java 7 although you can compile it successfully.
20th Dec 2016, 6:47 PM
Suprakash Pal
Suprakash Pal - avatar
0
you can run a java program without main method, you can visit for more details of list here https://www.mysoftkey.com/java/how-can-you-run-java-program-without-main-method/
24th Jan 2021, 2:10 PM
Ranjeet Jha
Ranjeet Jha - avatar