Skip to main content

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.

Alternate constructor invocation is the mechanism by which one constructor calls another overloaded constructor within the same class using the this() keyword. It facilitates constructor chaining, allowing a class to delegate object initialization to a specific, usually more comprehensive, constructor.
public class ServerConfig {
    public ServerConfig() {
        this(8080); // Alternate constructor invocation
    }

    public ServerConfig(int port) {
        // Target constructor logic
    }
}

Execution Flow

When a constructor invokes an alternate constructor via this(), the execution of the calling constructor is suspended. Control immediately transfers to the target constructor. The target constructor executes to completion—including the implicit or explicit invocation of the superclass constructor (super())—before control returns to the calling constructor to execute any subsequent statements.

Syntactic Rules and Compiler Constraints

To successfully compile and execute alternate constructor invocations, the Java compiler enforces strict rules regarding object state and execution order: 1. First Statement Requirement The this() invocation must be the absolute first executable statement within the constructor body. You cannot execute any logic, variable declarations, or print statements prior to calling this(). 2. Pre-Initialization Context When evaluating the arguments passed to this(), the object is in a pre-initialized state. Therefore, you cannot reference instance variables, instance methods, or the this reference itself within the argument list. You may pass any expression that does not depend on the current instance’s state. This includes literals, static variables, the results of static method calls, new object instantiations (e.g., new ArrayList<>()), class literals (e.g., String.class), and mathematical expressions.
public class Server {
    private int defaultPort = 80;

    public Server() {
        // COMPILATION ERROR: Cannot reference instance variable 'defaultPort' 
        // before supertype constructor has been called
        this(defaultPort); 
    }

    public Server(int port) {
        // Initialization logic
    }
}
3. Mutual Exclusion with super() A single constructor cannot contain both this() and super(). Because both are required to be the first statement in a constructor, they are mutually exclusive. When this() is used, the responsibility of invoking the superclass constructor is delegated to the target constructor in the chain. 4. Prohibition of Cyclic Invocation The compiler strictly prohibits recursive or cyclic constructor chaining. A constructor cannot invoke itself, nor can a chain of this() calls form a loop. This prevents infinite recursion during object instantiation.
public class Node {
    public Node() {
        this(10); // COMPILATION ERROR: Recursive constructor invocation
    }

    public Node(int value) {
        this();   // COMPILATION ERROR: Recursive constructor invocation
    }
}
Master Java with Deep Grasping Methodology!Learn More