From JLS
A class or interface type T will be initialized immediately before the first occurrence of any one of the following:
- T is a class and an instance of T is created.
- T is a class and a static method declared by T is invoked.
- A static field declared by T is assigned.
- A static field declared by T is used and the field is not a constant variable (§4.12.4).
- T is a top-level class, and an assert statement (§14.10) lexically nested within T is executed.
In the First case(first set of print staments), you have access only the sName
variable only, and it's belong to Parent
Class, so the child class was not initialized.
In the second set of print statements, You have access the sName1
variable, which belongs to Child
class, so at this time, Child class was initialized.
It doesn't matter, even you have accessed the Child.sName
, it actually refers the Parent.sName
, so it won't load the Child
class.