Can we execute a program without main() method?

Total Post:807

Points:4035

Java 
 1044  View(s)
Ratings:
Rate this:
  1. Can we execute a program without main() method?

    Yes, one of the way is static block but in previous version of JDK not in JDK 1.7


    1. class A3{  
    1.   static{  
    1.   System.out.println("static block is invoked");  
    1.   System.exit(0);  
    1.   }  
    1. }  

    Output:static block is invoked (if not JDK7)

    In JDK7 and above, output will be:

    Output:Error: Main method not found in class A3, please define the main method as:
    public static void main(String[] args)

Answer