Can we execute a program without main() method?
In Java we can execute the program without main method can we give some examples??
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Mobeen RashidRank #1,213 · 1.5K reputationPosted Jun 22, 2017, 10:55 PM EDT
Naveen ArumugamRank #1,227 · 1.5K reputationPosted Jun 27, 2017, 11:02 PM EDT
Vivek KumarRank #272 · 9.3K reputationPosted Jun 27, 2017, 11:14 PM EDT
Guest UserRank #4,500 · 340 reputationPosted Jun 28, 2017, 1:26 AM EDT
Manikandan MurugesanRank #300 · 8.4K reputationPosted Jun 28, 2017, 6:22 AM EDT
Rajkiran SwainRank #165 · 14.4K reputationPosted Jun 28, 2017, 7:51 AM EDT
Mukil VendhanRank #2,971 · 486 reputationPosted Aug 11, 2017, 6:57 AM EDT
Yes, sequence is as follows:
So, if there's code in a static block, it will be executed. But there's no point in doing that.
How to test that:
public final class Test { static { System.out.println("FOO"); } }Then if you try to run the class (either form command line with
java Testor with an IDE), the result is: