ReviseAlgo Logo

Object-Oriented Programming

Constructors

Analyze default vs parameterized constructors, constructor chaining via this and super, and initialization block execution sequences.

Interview: Evaluates constructor chaining order, default constructor insertion mechanics, and static/instance initialization block execution order.

Last Updated: June 13, 2026 10 min read

A Constructor is a special sub-routine called when an object is instantiated. It initializes the object's fields. Constructors can be overloaded and chained together using specific keywords.

Core Idea

Constructors initialize object state. If no constructor is defined, the compiler inserts a default no-arg constructor.

Why It Matters

Chaining constructors using this() or super() prevents code duplication and enforces parent initialization invariants.

Interview Lens

Focuses on the execution order of static blocks, instance blocks, parent constructors, and child constructors.

Constructor Chaining: this() and super()

Constructor Chaining is the process of calling one constructor from another:

  • this(): Invokes an overloaded constructor in the same class. Used to delegate initialization steps and provide default parameter configurations.
  • super(): Invokes a constructor in the parent class. If you don't call super() explicitly, the compiler automatically inserts a default, parameterless super() call as the first line of the constructor.
  • Constraint: this() or super() must be the very first statement in the constructor body. You cannot use both in the same constructor.

Execution Order of Initialization Blocks

When a class is loaded and instantiated, the JVM executes blocks in a strict sequence:

  1. Static Blocks / Static Field Initializers: Run once when the class is loaded, in the order they appear.
  2. Parent Initialization blocks & constructors: Runs before child components.
  3. Instance Initialization Blocks / Instance Field Initializers: Run every time an instance is created, before the constructor's code body executes.
  4. Constructor Code Body: Executes last.

Common Pitfalls

  • Chaining Loops: Creating circular dependencies (e.g. constructor A calls B, and B calls A), causing a compilation error.
  • Placing calls out of order: Placing statements before this() or super(), which results in compile-time errors.
  • Losing the Default Constructor: Defining a parameterized constructor and forgetting that the compiler will no longer insert the default no-arg constructor, breaking subclasses that rely on it.

Best Practices

  • Always define an explicit no-arg constructor if your class has parameterized constructors.
  • Chain overloaded constructors to a single primary constructor containing the core initialization logic.
  • Avoid calling non-final (overridable) methods inside constructors, as they can execute on partially initialized child objects.

Interview-Relevant Information

Q1: What is the execution order of initialization blocks when creating an object?
Answer: 1. Static blocks (only once per class loading). 2. Parent instance blocks and parent constructors. 3. Child instance blocks. 4. Child constructor code bodies.

Q2: Why must this() or super() be the first statement in a constructor?
Answer: This ensures that parent class components and overloaded initialization states are established before any child-specific class logic executes. This prevents using fields before they are initialized.

Quick Checklist

Can you trace complex initialization blocks, chain overloaded constructors, prevent circular chaining compile errors, and safely override parent parameters? If yes, you understand constructors.

Use Cases

Initializing complex database client connections in static blocks.

Enforcing structural dependency injection using constructors.

Common Mistakes

Attempting to write statements before invoking super() or this() inside constructors.

Triggering stack overflows by creating circular constructor delegating chains.