I have this simple piece of code.
abstract class X {
X() {
read();
}
private void read() {
Object obj = new Object();
readVa
The reason the obj field is null is due to the sequence of steps that occur in the constructor call of Y:
Y calls the super constructor which eventually calls readValue of the concrete class Y, therefore assigning a non-null value to the obj field.After the super constructor finishes, the instance field obj is initialized to null due to the variable initializer:
Object obj = null;
When you remove the null initializer, it becomes a simple field declaration with no instance initialization to be performed in step 2.
The apt solution is not to remove the null initializer, but to re-design the whole class hierarchy. For example, since the purpose of readValue seems to just be a setter for a variable, then you don't need to make it override an abstract method in the parent class. Just set it as a separate method and call it after the constructor of Y completes.