Fundamentals 4 min read

Understanding Java Constructor Execution Order

This article examines the Java constructor execution sequence, detailing how class initialization proceeds from zeroed memory allocation through static and instance variable initialization, parent constructor invocation, and finally the subclass constructor, illustrated with sample code and explanatory comments.

Qunar Tech Salon
Qunar Tech Salon
Qunar Tech Salon
Understanding Java Constructor Execution Order

Yesterday while modifying code I noticed the execution did not match expectations; further investigation revealed it was due to the constructor execution order, indicating a gap in my fundamentals.

Below is the test code (image omitted) and its execution result:

FatherProp is construct
Father is construct
SonProp is construct
Son is construct

The Java class initialization follows these steps:

(1) Allocate memory for the object, initializing fields to zero or null. (2) Execute initialization expressions for parent class fields and instance variables in order. (3) Invoke the parent class constructor (or the specific one if super() is used). (4) Execute initialization expressions for the current class's fields and instance variables. (5) Invoke the current class's constructor.

Comments:

1. Initialization is divided into static initialization and instance initialization. 2. Each class in the JVM corresponds to a Class instance. 3. The parent class instance exists as part of the child class instance (Class instances also have parent‑child relationships). 4. The class must be initialized before its instances.

Based on the above points, the overall initialization order can be understood as:

1. Parent class static fields and static blocks (parent class initialization, JVM cinit()). 2. Child class static fields and static blocks (child class initialization, JVM cinit()). 3. Parent class instance construction and instance variable initialization (instance variables placed in JVM init()). 4. Child class instance construction and instance variable initialization (also in init()).

Each class corresponds to a single Class instance in the JVM; therefore static methods and fields belong to that Class instance, which is why they are shared across all object instances.

Regarding the parent instance being part of the child, similar to C++ object layout, the parent instance occupies the beginning address of the child instance, making upcasting safe because the memory region up to the size of the parent represents the parent object.

JavaJVMobject-orientedInitializationConstructor
Qunar Tech Salon
Written by

Qunar Tech Salon

Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.