I have a question regarding static blocks:
Let's say I have a class similar to this:
class SomeClass { static { System.out.println("static block"); } }
and somewhere I define a variable of type SomeClass .
public static void main(String args[]) { SomeClass foo; }
Now I thought that the static block would be executed, but it is not. As far as I know, a static block is executed as soon as the class loader loads the SomeClass class. Now to my real question:
Is the class loading as soon as I define a variable of this type? . If so, why is the static block not executing?
If the answer should be negative, then how can I find out if the class has already been loaded by the class loader and what are the different options for loading the class (I know that 2: initializing the variable and using a static field / method)
java static-block
Parkerhalo
source share