> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Java Superclass Constructor Invocation

Superclass constructor invocation is the mechanism by which a subclass constructor explicitly or implicitly calls the constructor of its immediate parent class during object instantiation. This process ensures that the inherited state of an object is properly initialized before the subclass-specific initialization occurs, establishing a strict top-down initialization sequence known as constructor chaining.

In Java, this invocation is handled using the `super` keyword followed by parentheses containing any required arguments.

```java theme={"dark"}
class SuperClass {
    public SuperClass(int arg1) {
        // Superclass initialization logic
    }
}

public class SubClass extends SuperClass {
    public SubClass(int arg1, String arg2) {
        super(arg1); // Explicit superclass constructor invocation
    }
}
```

## Core Mechanics and Rules

**1. Placement and Flexible Constructor Bodies**
Historically (Java 21 and earlier), an explicit superclass constructor invocation was required to be the absolute first statement within the subclass constructor's body. Starting with Java 22 (JEP 447) and Java 23 (JEP 482) via "Flexible Constructor Bodies," Java allows pre-construction statements *before* the `super()` call. These statements can include local variable declarations, argument validation, or any logic that does not reference the instance being created.

**2. Pre-Construction Context**
Arguments passed to `super(...)` are evaluated in a strict pre-construction context. Because the superclass has not yet been initialized, these arguments cannot reference the current instance (`this`), instance variables, or instance methods of the subclass being instantiated. However, Java allows *any* expression that does not depend on the current instance. This includes parameters passed into the subclass constructor, static variables, static methods, literals (e.g., `super(10)` or `super("text")`), object allocations (e.g., `super(new ArrayList<>())`), and method calls on independent objects.

**3. Implicit Invocation**
If a subclass constructor does not explicitly invoke a superclass constructor via `super(...)`, and does not invoke an overloaded constructor in the same class via `this(...)`, the Java compiler automatically inserts a no-argument `super();` call.

```java theme={"dark"}
class Parent {
    public Parent() {
    }
}

// Developer-written code:
class ImplicitChild extends Parent {
    public ImplicitChild() {
        System.out.println("Child initialized");
    }
}

// Compiler-generated equivalent:
class ExplicitEquivalentChild extends Parent {
    public ExplicitEquivalentChild() {
        super(); // Implicitly inserted by the compiler
        System.out.println("Child initialized");
    }
}
```

**4. Compilation Dependencies**
Because of the implicit invocation rule, if a superclass defines only parameterized constructors (meaning it lacks a no-argument constructor), the subclass *must* explicitly invoke one of those parameterized constructors using `super(args)`. Failure to do so causes a compilation error because the compiler's implicitly inserted `super();` will fail to resolve to an existing constructor in the parent class.

**5. Mutual Exclusivity with `this()`**
A constructor can contain either an explicit superclass constructor invocation (`super()`) or an explicit alternate constructor invocation (`this()`), but never both. If `this()` is used, the superclass constructor invocation is deferred to the target constructor being called within the same class.

## Qualified Superclass Constructor Invocation

When a class extends a non-static inner class, the inner class relies on a hidden reference to its enclosing instance. To instantiate the subclass, the Java compiler requires an explicit reference to the enclosing class. This is handled via a Qualified Superclass Constructor Invocation using the syntax `enclosingInstance.super(...)`.

```java theme={"dark"}
class Outer {
    class Inner {
        Inner() {}
    }
}

class SubInner extends Outer.Inner {
    // The subclass constructor must be provided an instance of the enclosing class
    SubInner(Outer enclosingInstance) {
        enclosingInstance.super(); // Qualified superclass constructor invocation
    }
}
```

## Constructor Chaining Execution Order

When a subclass is instantiated, the constructor invocation propagates up the inheritance hierarchy until it reaches the root `java.lang.Object` class. The actual execution of initialization logic happens in a strict top-down order. For every class in the hierarchy, starting from `Object` and moving down to the instantiated subclass, the following sequence occurs:

1. **Superclass Constructor Execution:** The superclass constructor body executes (propagating from `Object` downwards).
2. **Instance Initialization:** Immediately after the superclass constructor returns, the current class's instance variable initializers and instance initialization blocks execute in their textual order.
3. **Subclass Constructor Execution:** Finally, the remainder of the current subclass constructor body executes.

This guarantees that a parent class is fully initialized—including its variables and initialization blocks—before the child class's initialization blocks or constructor body attempt to access or modify the inherited state.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Java Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
