here is the code
public class ClassResolution {
static class Parent {
public static String name;
static {
System.out.println(\"this is Parent
In short, Child.name
is equal to Parent.name
, and the compiler compiles it like so.
Because name
is a static field of the Parent class, it is a class method in Parent, not Child. The Java compiler allows a shortcut where child classes can call static parent class methods/fields as if they come from their own class, but internally they are compiled with regards to the parent class.
Although your code refers to Child.name
, it is internally Parent.name
, as handled by the compiler. Then, because the Child class isn't initialized, the
static initializer block is never run, and name
remains "Parent".