Access to fields (unlike method calls) is not subject to dynamic scheduling of runtime, they are allowed solely on the basis of compilation time types.
The variable b has the Base compilation time type, therefore b.getx() also refers to the Base compilation time type, and therefore b.getx().x will be compiled to access the base field x , not the child. This is confirmed by looking at the javap output for the main method:
public static void main(java.lang.String[]); Code: 0: new #3;
you can see that b.getx().x was compiled into the getfield command for Base.x
source share