Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Java this() Constructor Chaining: Rules, Examples, and Pitfalls

Learn how to use Java's this(...) constructor delegation to call one constructor from another, with placement rules, examples, initialization order, and common pitfalls.

~5 min readJava
#Java#constructor chaining#this keyword#overloaded constructors
A Java constructor calling another constructor using this(), with a chain of overlapping constructor blocks and a clear delegation arrow.

In Java, the this keyword has two distinct uses: it refers to the current object, and, inside a constructor, it can call another constructor of the same class. The constructor-call form is written as this(...) (or this() for a no-argument constructor) and is a common way to reduce constructor duplication. This article explains the placement rules, shows practical examples, and highlights the mistakes that cause compilation errors or confusing initialization behavior.

The Syntax and Placement Rules for this(...)

A constructor call via this(...) must be the first statement in a constructor. If any other statement appears before it, the compiler rejects the code. This restriction ensures that constructor delegation always happens before the rest of the calling constructor's body runs.

java
public class User { private String name; private int age; public User(String name) { this(name, 0); // must be first } public User(String name, int age) { this.name = name; this.age = age; } }

The this(...) call is resolved by overload resolution against the constructors declared in the same class. It cannot call a superclass constructor. The compiler also rejects recursive or circular constructor chains, so a this(...) chain must eventually reach a constructor that does not delegate further.

A Practical Example: Overloaded Constructors with Default Values

A common use case is providing default values when a caller omits parameters. Instead of duplicating assignment logic in every overload, delegate to the most complete constructor.

java
public class Order { private String id; private double amount; private String status; public Order(String id) { this(id, 0.0, "DRAFT"); } public Order(String id, double amount) { this(id, amount, "DRAFT"); } public Order(String id, double amount, String status) { this.id = id; this.amount = amount; this.status = status; } }

Here, the one-argument and two-argument constructors both delegate to the three-argument constructor. This keeps validation and assignment logic in one place. If you later add a new field, you only need to update the full constructor, and the shorter overloads remain correct.

Common Mistakes and Compilation Errors

The most frequent mistake is trying to call this(...) after performing some operation. The compiler error "call to this must be first statement in constructor" is clear. Another mistake is using this as a reference inside the argument list, such as this(this.name), which is invalid because this is not yet fully initialized at that point.

A more subtle error is creating a circular chain. For example, constructor A calls this(...) to constructor B, and B calls back to A. The compiler reports a recursive constructor invocation error. Always design the chain so it terminates in a constructor that does not delegate further.

Runtime Behavior and Initialization Order

When a constructor calls this(...), the delegated constructor runs completely before the calling constructor's body executes. This means any field initializers and instance initializer blocks in the class run once, before the delegated constructor body. The order is:

  1. Field initializers and instance initializer blocks, in source order
  2. The delegated constructor body
  3. The calling constructor body, after the this(...) call returns

Consider this example:

java
public class Product { private String code; private int stock = 10; public Product(String code) { this(code, 100); System.out.println("after delegation"); } public Product(String code, int stock) { this.code = code; this.stock = stock; System.out.println("delegated constructor"); } }

Creating new Product("A") prints:

text
delegated constructor after delegation

This ordering matters when you rely on side effects or when the delegated constructor performs expensive setup. If you need to run logic before delegation, you cannot do it inside the constructor; consider a static factory method instead.

this(...) with Inheritance and Final Fields

Constructor chaining with this(...) is limited to the same class. You cannot use this(...) to call a superclass constructor; that requires super(...). The two calls are mutually exclusive: a constructor can start with either this(...) or super(...), but not both.

Final fields also interact with delegation. A final field must be assigned exactly once during construction. If a final field is assigned in a constructor and that constructor delegates to another one, the delegated constructor is where the assignment must happen. You cannot assign it in both constructors, and it cannot be left unassigned.

java
public class Config { private final String name; public Config() { this("default"); // final field assigned here } public Config(String name) { this.name = name; } }

If the delegated constructor does not assign the final field, compilation fails. This is a common source of errors when refactoring constructors to use this(...).

When Constructor Chaining Is the Right Choice

Constructor chaining via this(...) is ideal when you have several overloads that share initialization logic and differ only in default values or optional parameters. It keeps the code DRY and reduces the chance of inconsistent field assignments.

However, if the overloads perform fundamentally different setup or require different validation rules, forcing them into a chain can obscure the intent. In such cases, a static factory method with descriptive names may be clearer.

ApproachUse CaseTradeoff
this(...) chainingOverloads with shared initializationSimple, but limited to same class
Static factoryComplex creation logic or named alternativesMore flexible, but adds a method layer

For example, a factory method User.createAdmin() can call a private constructor with specific arguments, while a regular User constructor handles the common path. This avoids exposing overloads that might be misused.

Maintainability and Code Clarity Considerations

Constructor chaining reduces duplication, but it also introduces a dependency between constructors. If you change the parameter list of the full constructor, you must update every this(...) call. This is manageable when the chain is short and the overloads are stable.

A practical rule is to keep the delegated constructor as the most complete one, and have shorter overloads delegate to it. Avoid delegating to a shorter constructor from a longer one, because that would require passing extra arguments that are then ignored, which is confusing.

Another consideration is readability. When a constructor has many parameters, a chain of this(...) calls can be hard to follow. In that case, consider using a builder pattern or a single constructor with a parameters object, especially if the number of parameters exceeds four or five.

Anonymous Classes and Constructor Delegation

Anonymous classes cannot have explicit constructors, so this(...) is not applicable there. If you need constructor-like logic in an anonymous class, use an instance initializer block or a factory method. Local classes, in contrast, can declare explicit constructors, so normal constructor chaining with this(...) works there.

The usual placement rule still applies: a constructor may begin with either this(...) or super(...), but not both. Remember that this(...) is constructor delegation, not a method call, and it must be the first statement in the constructor.

Try matching browser tools

All tools

Small utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Java guides

More in Java

All Java guides

The guides filed next to this one in Java, in library order.