I was under the impression that a class's static initialization block gets called when the class is loaded.
(For example see this answer: https://stackoverflow.com/a/9130560/889742 )
But this test shows that the static block is not called at class load time, but later at first use time.
Why?
class Test
{
static
{
System.out.println("In test static block");
}
static int x;
}
public class xxxx {
public static void main(String[] args) throws Exception {
Class<?> clasz = ClassLoader.getSystemClassLoader().loadClass("Test");
//at least one of these lines is required for static block to be called
//Test.x = 1;
//clasz.newInstance();
}
}