Why aren't fields initialized to non-default values when a method is run from super()?
inheritance, java
Solution
boolean bool = true;
public ClassTwo() {
super();
}
is identical to
boolean bool;
public ClassTwo() {
super();
bool = true;
}
The compiler automatically moves the fields initializations within the constructor (just after the super constructor call, implicitly or explicitly).
Since a boolean field default value is `false`, when `super()` is called (and thus `ClassOne()` and `fireMethod()`), `bool` hasn't been set to `true` yet.
Fun fact: the following constructor
public ClassTwo() {
super();
fireMethod();
}
will be understood as
public ClassTwo() {
super();
bool = true;
fireMethod();
}
by the JVM, and the output will thus be
bool=false
bool=true
Problem
I must have spent over an hour trying to figure out the reason for some unexpected behavior. I ended up realizing that a field wasn't being set as I'd expect. Before shrugging and moving on, I'd like to understand why this works like this. In running the example below, I'd expect the output to be true, but it's false. Other tests show that I always get whatever that type default value is. ``` public class ClassOne { public ClassOne(){ fireMethod(); } protected void fireMethod(){ } } public class ClassTwo extends ClassOne { boolean bool = true; public ClassTwo() { super(); } @Override protected void fireMethod(){ System.out.println("bool="+bool); } public static void main(String[] args) { new ClassTwo(); } } ``` output: ``` bool=false ```