Variable is null at super call

When you call new TestNull(); you're calling the constructor of the class TestNull, which it calls the super() constructor: it contains a call to the method implemented in TestNull, where you print the String field, at this time the fields of the sub-class TestNull are not yet initialized, i.e. are null.

After the super constructor call, all the fields will be initialized, and therefore the second print actually show the new value of the (initialized) string.

The key point here is that fields of a sub-class are initialized after the instantiation of the super-classes.

A workaround? It depends on what exact behaviour you desire: maybe it makes sense to NOT call the abstract method in the super constructor (i.e. in the constructor of the TestSuper class).


According to JLS 8.1.1.1 Abstract Class

A subclass of an abstract class that is not itself abstract may be instantiated, resulting in the execution of a constructor for the abstract class and, Therefore, the execution of the Field Initializers for instance variables of that class.