Can we create a program without main method ?

By John

This is a Java Program to Print any Statement Without Using the Main Method.
We put the statement to be printed in a static block. The static block executes before main method hence we get the desired output. But in case of JDK7 the code would not execute as it looks for main method before any other thing.

Here is the source code of the Java Program to print any Statement Without Using the Main Method. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

public class Without_Main
System.out.println(“Hello World!!”);
//Does not work with JDK7

$ javac
$ java Without_Main

Hello World!!