public class Main {
public static void main(String[] args) {
System.out.println(B.x);
}
}
class A {
public static String x = "x";
}
class B extends A {
static {
System.out.print("Inside B.");
}
}
Question: Why output will be: x. But not: Inside B.x
The reference to
B.xissues the following bytecode:According to Java Virtual Machine Spec
So the JVM should resolve the symbolic reference to
B.x. The field resolution is specified like this:In other words the JVM will resolve
B.xintoA.x. This is why onlyAclass needs to be loaded.