static variables or static blocks in a class will be initialised before the class gets instantiated. Also, we all know that the static variables are not tied up with the instances. So even if we have the reference as null, the variable s value had already been initalised to "hello". So calling it that way will print the values Inside Method and Hello as the variable call will be made with the class name as they are tied up with the class . If you make the variable non - static , you will get a NPE.
静态变量和静态块在类被实例化之前,已经得到初始化。
本文探讨了静态变量和静态块在类实例化之前的初始化过程。静态变量独立于类实例存在,即使对象引用为null,静态变量仍会被初始化并持有其初始值。如果将变量声明为非静态,则在尝试访问前必须先创建类的实例。
343

被折叠的 条评论
为什么被折叠?



