Static block is not executing in JDK 7, “Main method not found”, but works in JDK 1.5

前端 未结 9 2163
我寻月下人不归
我寻月下人不归 2021-01-07 20:19

I have written a simple class with one static block

class Hello
{

  static {
           System.out.println(\"Hello\");
       System.exit(0);
     }
}
         


        
9条回答
  •  半阙折子戏
    2021-01-07 20:57

    Upto JDK1.6:-

    It first load the class and while loading the class static block will be executed. and then check the main method to execute.

    JDK1.7 onwards :

    It checks main method first whether it is available or not.

    if available
       then first execute static and
       then main method will be executed.
    
    if not available
    throw error 
    

提交回复
热议问题