1.

Execute a Java Program without main() method

Answer»

A JAVA program can be compiled and executed without MAIN method if we use the static block having System.exit(0); statement at the end which terminates the program before JVM starts looking for main method. However, this is only possible upto Java 6 .

Let us see how to execute a Java program without the main() method

public class Example { static {  System.out.println("HELLO World");  System.exit(0); } }

Upto Java 6, the output would be as follows:

$javac Example.java $java Example Hello World

Java 7 onwards, the program gets compiled but will not get executed and will show an error message as.



Discussion

No Comment Found