Reputation: 131
NB: I added the android
tag because I've only tried this on Android's own VM, as part of an app. Other JVMs might or might not behave the same way.
Here's a simplified version of my code:
public class Test {
public static class Base {
public final Object base = new Object();
public Base() {
print();
}
public void print() {
System.err.println(String.format("base = %s", base));
}
}
public static class Outer extends Base {
public final Object member = new Object();
public Outer() {
super();
}
@Override
public void print() {
super.print();
System.err.println(String.format("member = %s", member));
}
}
public static void main(String[] args) {
new Outer();
}
}
Output:
base = java.lang.Object@9a9e0fa
member = null
Why is member
NULL? I was under the impression any nonstatic class fields are initialized before the constructor runs.
Upvotes: 0
Views: 222
Reputation: 718718
I was under the impression any nonstatic class fields are initialized before the constructor runs.
Your impression is / was incorrect.
What actually happens is that all superclass initialization for an object happens before any subclass initialization for the object.
The above rule-of-thumb is for object instance initialization only. Class (i.e. static
) initialization is a bit more complicated. But this question is about instance initialization.
The process is described in detail in JLS 12.5. The relevant part is this:
Just before a reference to the newly created object is returned as the result, the indicated constructor is processed to initialize the new object using the following procedure:
Assign the arguments for the constructor to newly created parameter variables for this constructor invocation.
If this constructor begins with an explicit constructor invocation (§8.8.7.1) of another constructor in the same class (using this), then evaluate the arguments and process that constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason; otherwise, continue with step 5.
This constructor does not begin with an explicit constructor invocation of another constructor in the same class (using this). If this constructor is for a class other than Object, then this constructor will begin with an explicit or implicit invocation of a superclass constructor (using super). Evaluate the arguments and process that superclass constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, continue with step 4.
Execute the instance initializers and instance variable initializers for this class, assigning the values of instance variable initializers to the corresponding instance variables, in the left-to-right order in which they appear textually in the source code for the class. If execution of any of these initializers results in an exception, then no further initializers are processed and this procedure completes abruptly with that same exception. Otherwise, continue with step 5.
Execute the rest of the body of this constructor. If that execution completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, this procedure completes normally.
Note the sentence that I have highlight. When you take that in the context of the rest, that means that the constructor body for Base()
runs before the initialization of the instance fields for Outer
.
Other JVMs might or might not behave the same way.
In fact all JVMs should behave this way. This is how the Java language is specified to behave.
Upvotes: 3