Monday 6 June 2016

Can we write a Java program without main() method ?

Yes, We can write a java program without main method. it is possible by using static block only, which was supported in java 1.6 version. But not possible in Java 7 or Java 8.

class Test{  
  static{  
  System.out.println("Silan Software");  
  System.exit(0);  
  }  
}  

Output 
Silan Software (if not JDK7)


In JDK7 and above, the output will be:
Error: Main method not found in class Test, please define the main method as:public static void main(String[] args)


No comments:

Post a Comment