The reference to B.x
issues the following bytecode:
getstatic #3 <Field int B.x>
According to Java Virtual Machine Spec
The Java virtual machine instructions anewarray, checkcast, getfield,
getstatic, instanceof, invokedynamic, invokeinterface, invokespecial,
invokestatic, invokevirtual, ldc, ldc_w, multianewarray, new,
putfield, and putstatic make symbolic references to the runtime
constant pool. Execution of any of these instructions requires
resolution of its symbolic reference.
So the JVM should resolve the symbolic reference to B.x
. The field resolution is specified like this:
To resolve an unresolved symbolic reference from D to a field in a
class or interface C, the symbolic reference to C given by the field
reference must first be resolved (§5.4.3.1).
...
When resolving a field reference, field resolution first attempts to
look up the referenced field in C and its superclasses:
If C declares a field with the name and descriptor specified by the
field reference, field lookup succeeds. The declared field is the
result of the field lookup.
Otherwise, field lookup is applied recursively to the direct
superinterfaces of the specified class or interface C.
Otherwise, if C has a superclass S, field lookup is applied
recursively to S.
Otherwise, field lookup fails.
In other words the JVM will resolve B.x
into A.x
. This is why only A
class needs to be loaded.