Sub topic 11
Field and Constructor Initialization Order
When you create an object, Java fills in its fields and runs its constructors in a set order. Knowing that order helps you understand what values are available at each step.
A field is a variable stored in an object. A constructor is code that runs when an object is created. A superclass is a class that another class builds on. The class built on it is called a subclass.
The order
When you create a subclass object, Java follows these steps:
- Give all the object’s fields their default values. For example, an
intfield starts at0, abooleanfield starts atfalse, and a reference field starts atnull. - Run the superclass’s construction steps.
- Run the subclass’s field initializers, in the order they appear in the class.
- Run the rest of the subclass constructor.
A field initializer is an expression that gives a field a value where the field is declared. For example, int count = 5; has the initializer 5.
The superclass follows the same pattern: its own superclass is constructed first, then its field initializers and constructor run. This continues back to Object, the class at the top of the chain.
Follow an example
Save this program as Main.java:
class Parent {
int parentValue = Main.show("Parent field", 10);
Parent() {
System.out.println("Parent constructor: " + parentValue);
}
}
class Child extends Parent {
int childValue = Main.show("Child field", 20);
Child() {
super();
System.out.println("Child constructor: " + childValue);
}
}
public class Main {
static int show(String label, int value) {
System.out.println(label + ": " + value);
return value;
}
public static void main(String[] args) {
Child child = new Child();
System.out.println("Ready: " + child.childValue);
}
}The show method prints a label and value, then returns that value for the field. The word static lets the program call this helper using the class name, Main.
Compile and run the program:
javac Main.java
java MainThe output is:
Parent field: 10
Parent constructor: 10
Child field: 20
Child constructor: 20
Ready: 20First, both fields receive the default value 0. Java then runs the construction steps for Object, which print nothing here. Next, the Parent field initializer sets parentValue to 10, and the Parent constructor prints it.
Only after that does the Child field initializer set childValue to 20. The rest of the Child constructor then runs. Finally, the completed object is available to main.
The call super() starts superclass construction. If you leave it out, Java normally adds a no-argument super() call for you. That requires the superclass to have an accessible no-argument constructor.
Other initialization steps
A class can also contain an instance initializer block: a block of code inside the class that runs when an object is created. In each class, these blocks and the field initializers run in the order they appear. They run after superclass construction and before the rest of that class’s constructor.
A constructor can use this(...) to call another constructor in the same class. The field initializers run only once for the object. After the called constructor finishes, the remaining code in the calling constructor runs.
Check your understanding
Exercise
In the example, what is childValue while the Parent constructor is running, and why?
Show answer
It is 0. Java has given the field its default value, but the Child field initializer has not run yet.