CAN WE EXECUTE A PROGRAM WITHOUT MAIN() METHOD?

Samuel Fernandes

Total Post:38

Points:190
Posted by  Samuel Fernandes
Java 
 618  View(s)
Ratings:
Rate this:
  1. Mayank Tripathi

    Post:807

    Points:4035
    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

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!