Home > DeveloperSection > Interviews > Can we execute a program without main() method?

Posted on    March-27-2015 12:12 AM

 JavaScript Java 
 1 Answer(s)
  579  View(s)
Rate this:

Mayank Tripathi
Mayank Tripathi

Total Post:807

Posted on    March-27-2015 1:18 AM

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)

Don't want to miss updates? Please click the below button!

Follow MindStick