Is It Possible to Run a Java Program Without Main Method?

It is mostly said that running a program is not possible without main() method. In most colleges and institutes and organizations, teachers and interviewers asked if it is possible to execute a Java program without main(). The answer should be Yes. You might have noticed that Java applets do not have main() but run on browsers.

A Java program can run without the main method. You need a static block for that, You just need to put your executable code in that static block and execute it. The sequence goes something like this:

  • The Java Virtual Machine first loads your class
  • Then it arranges and runs all static blocks
  • Then finally stare across your code for main() and uses it

However, if you are planning to run your program directly from the command-line without using static block, and with the use of JVM, then your program must be required to have the main() method. This is because the Java Virtual Machine keeps looking for the main().


Here are few other related articles for you to read:

Courses
Subscribe Updates via Email

Join 49,000+ W3schools lovers and get all the latest tutorials, programs, algorithms in your inbox.