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

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

    The program doesn't execute because from JDK 1.7 oracle has put the restriction on static block and static variable calling method if your program doesn't have main method with proper signature but static block and method will always be execute first.As memory management of static variables are done at the time of class loading.

提交回复
热议问题