Can we run define Java class without main() method?



No, We cannot define a class without main() method starting from Java 7.
In the previous release of Java, we can have Static Initalizers as an alternative:
public class MainMethodDemo
{
    static
    {
        System.out.println("Static Initalizer");
        System.exit(0);
    }
}
Output: (From JDK 7)
Error: Main method not found in class Test, please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
Output: (JDK 6)
Static Initalizer

Comments