Is the Main method must needed in a Java program?

The main method is not needed in java programs. As others have pointed out, web applications do not use the main method.

It is not even needed in standalone applications. Consider

class JavaAppWithoutMain
{
    static
    {
    System . out . println ( "Hello World!" ) ;
    }
}

I compiled it and ran and obtained the following result:

Hello World!
Exception in thread "main" java.lang.NoSuchMethodError: main

For standalone applications you must either have

  1. a main method or
  2. a static initializer.

Main is preferred.

Leave a Comment