Can you explain how method chaining works in the Builder pattern?

Method chaining in the Builder pattern works by having each setter-style method return the builder instance itself, so multiple configuration calls can be strung together into a single fluent expression that ends with a build() call.

Key Points

Example

Calling `new PersonBuilder().name("Amol").age(30).city("Pune").build()` reads naturally as a single statement, where each method call configures one field and hands control back to continue the chain.

class PersonBuilder {
    private String name;
    private int age;

    PersonBuilder name(String name) {
        this.name = name;
        return this;
    }

    PersonBuilder age(int age) {
        this.age = age;
        return this;
    }

    Person build() {
        return new Person(name, age);
    }
}

Interview Tip

A concise interview answer is

Method chaining works because every builder method sets a field and returns the builder object itself, letting you string calls together into one fluent expression that finishes with build(), which constructs the final object — it reads cleanly and avoids constructors with long parameter lists.

Last updated: 16 July 2026

Previous question: What are the benefits of using the Builder pattern for constructing complex objects?

Next question: Provide an example of when using a Builder pattern is preferable over multiple constructors.

More Java Design Patterns interview questions

All Java Design Patterns questions