I came across the following code
class Super {
static String ID = "QBANK";
}
class Sub extends Super {
static {
System.out.print("In Sub");
}
}
public class Test{
public static void main(String[] args){
System.out.println(Sub.ID);
}
}
We refer to the class Subas Sub.id. Therefore, I expected the static block in the Sub-class to be executed and "In Sub" to be printed, but, to my surprise, it is not . Although the variable identifier is seen for the Sub class from the Super class, since we are referring to the variable using the Sub class, I expect it to be loaded first. And if it loads, a static block must be executed.
Since it doesn't print that the subclass is not loaded at all? How is this possible when I clearly refer to this in my code. I mean, how can a class be recognized without loading it into memory? Is something missing?