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

前端 未结 9 2165
我寻月下人不归
我寻月下人不归 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 21:19

    Java 7 looks for a main method before loading the class. This is a behavior change from previous java versions and hence your static block is not executing. In previous versions, the behavior was that JRE used to look for main method post loading the class and after executing the static blocks.

提交回复
热议问题