w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Accessing parent class static field using child class name doesn't load the child class?

When you access the static fields of a super class on subclass reference, only the class that declares the field will be loaded and initialized, in this case it is A. This is specified in JLS §12.4.1 - When Initialization Occurs:

A reference to a static field (ยง8.3.1.1) causes initialization of only the class or interface that actually declares it, even though it might be referred to through the name of a subclass, a subinterface, or a class that implements an interface.

Emphasis mine.

So in your code, class B would not even be initialized, and hence its static block would not be executed.

© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.