Fields that have a static modifier in their declaration are called static fields or class variables . They are associated with the class, and not with any object. Each instance of a class has a class that resides in one fixed location in memory. Any object can change the value of a class variable, but class variables can also be manipulated without instantiating the class.
So, when you call Zx
as below:
System.out.println(Zx);
It will not initialize the class, except when you call it Zx
, it will get this x
from this fixed memory.
The static block starts when the JVM loads class Z
Which is never loaded here because it can access this x
directly without loading the class.
source share