Below are some reasons arguing for the use of the pattern and example code in Java, but it is an implementation of the Builder Pattern covered by the Gang of Four in Design Patterns. The reasons you would use it in Java are also applicable to other programming languages as well.

As Joshua Bloch states in Effective Java, 2nd Edition:

The builder pattern is a good choice when designing classes whose constructors or static factories would have more than a handful of parameters.

We've all at some point encountered a class with a list of constructors where each addition adds a new option parameter:

Pizza(int size) { ... }        
Pizza(int size, boolean cheese) { ... }    
Pizza(int size, boolean cheese, boolean pepperoni) { ... }    
Pizza(int size, boolean cheese, boolean pepperoni, boolean bacon) { ... }

This is called the Telescoping Constructor Pattern. The problem with this pattern is that once constructors are 4 or 5 parameters long it becomes difficult to remember the required order of the parameters as well as what particular constructor you might want in a given situation.

One alternative you have to the Telescoping Constructor Pattern is the JavaBean Pattern where you call a constructor with the mandatory parameters and then call any optional setters after:

Pizza pizza = new Pizza(12);
pizza.setCheese(true);
pizza.setPepperoni(true);
pizza.setBacon(true);

The problem here is that because the object is created over several calls it may be in an inconsistent state partway through its construction. This also requires a lot of extra effort to ensure thread safety.

The better alternative is to use the Builder Pattern.

public class Pizza {
  private int size;
  private boolean cheese;
  private boolean pepperoni;
  private boolean bacon;

  public static class Builder {
    //required
    private final int size;

    //optional
    private boolean cheese = false;
    private boolean pepperoni = false;
    private boolean bacon = false;

    public Builder(int size) {
      this.size = size;
    }

    public Builder cheese(boolean value) {
      cheese = value;
      return this;
    }

    public Builder pepperoni(boolean value) {
      pepperoni = value;
      return this;
    }

    public Builder bacon(boolean value) {
      bacon = value;
      return this;
    }

    public Pizza build() {
      return new Pizza(this);
    }
  }

  private Pizza(Builder builder) {
    size = builder.size;
    cheese = builder.cheese;
    pepperoni = builder.pepperoni;
    bacon = builder.bacon;
  }
}

Note that Pizza is immutable and that parameter values are all in a single location. Because the Builder's setter methods return the Builder object they are able to be chained.

Pizza pizza = new Pizza.Builder(12)
                       .cheese(true)
                       .pepperoni(true)
                       .bacon(true)
                       .build();

This results in code that is easy to write and very easy to read and understand. In this example, the build method could be modified to check parameters after they have been copied from the builder to the Pizza object and throw an IllegalStateException if an invalid parameter value has been supplied. This pattern is flexible and it is easy to add more parameters to it in the future. It is really only useful if you are going to have more than 4 or 5 parameters for a constructor. That said, it might be worthwhile in the first place if you suspect you may be adding more parameters in the future.

I have borrowed heavily on this topic from the book Effective Java, 2nd Edition by Joshua Bloch. To learn more about this pattern and other effective Java practices I highly recommend it.

Answer from user2628 on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › implement the builder pattern in java
Implement the Builder Pattern in Java | Baeldung
April 22, 2024 - The Builder Pattern in Java 8 offers streamlined object construction and improved code readability. With variants like Classic, Generic, and Lombok Builder Patterns, we can tailor our approach to our specific needs.
🌐
Project Lombok
projectlombok.org › features › Builder
@Builder
If true, lombok will use guava's ImmutableXxx builders and types to implement java.util collection interfaces, instead of creating implementations based on Collections.unmodifiableXxx. You must ensure that guava is actually available on the classpath and buildpath if you use this setting.
🌐
GeeksforGeeks
geeksforgeeks.org › java › builder-pattern-in-java
Builder Method Design Pattern in Java - GeeksforGeeks
July 11, 2025 - The setter methods will now return Builder class reference. We will also have a build method to return instances of Server side class, i.e. outer class. ... // Java code to demonstrate Builder Pattern // Server Side Code final class Student { // final instance fields private final int id; private final String name; private final String address; public Student(Builder builder) { this.id = builder.id; this.name = builder.name; this.address = builder.address; } // Static class Builder public static class Builder { // instance fields private int id; private String name; private String address; pub
Top answer
1 of 13
1162

Below are some reasons arguing for the use of the pattern and example code in Java, but it is an implementation of the Builder Pattern covered by the Gang of Four in Design Patterns. The reasons you would use it in Java are also applicable to other programming languages as well.

As Joshua Bloch states in Effective Java, 2nd Edition:

The builder pattern is a good choice when designing classes whose constructors or static factories would have more than a handful of parameters.

We've all at some point encountered a class with a list of constructors where each addition adds a new option parameter:

Pizza(int size) { ... }        
Pizza(int size, boolean cheese) { ... }    
Pizza(int size, boolean cheese, boolean pepperoni) { ... }    
Pizza(int size, boolean cheese, boolean pepperoni, boolean bacon) { ... }

This is called the Telescoping Constructor Pattern. The problem with this pattern is that once constructors are 4 or 5 parameters long it becomes difficult to remember the required order of the parameters as well as what particular constructor you might want in a given situation.

One alternative you have to the Telescoping Constructor Pattern is the JavaBean Pattern where you call a constructor with the mandatory parameters and then call any optional setters after:

Pizza pizza = new Pizza(12);
pizza.setCheese(true);
pizza.setPepperoni(true);
pizza.setBacon(true);

The problem here is that because the object is created over several calls it may be in an inconsistent state partway through its construction. This also requires a lot of extra effort to ensure thread safety.

The better alternative is to use the Builder Pattern.

public class Pizza {
  private int size;
  private boolean cheese;
  private boolean pepperoni;
  private boolean bacon;

  public static class Builder {
    //required
    private final int size;

    //optional
    private boolean cheese = false;
    private boolean pepperoni = false;
    private boolean bacon = false;

    public Builder(int size) {
      this.size = size;
    }

    public Builder cheese(boolean value) {
      cheese = value;
      return this;
    }

    public Builder pepperoni(boolean value) {
      pepperoni = value;
      return this;
    }

    public Builder bacon(boolean value) {
      bacon = value;
      return this;
    }

    public Pizza build() {
      return new Pizza(this);
    }
  }

  private Pizza(Builder builder) {
    size = builder.size;
    cheese = builder.cheese;
    pepperoni = builder.pepperoni;
    bacon = builder.bacon;
  }
}

Note that Pizza is immutable and that parameter values are all in a single location. Because the Builder's setter methods return the Builder object they are able to be chained.

Pizza pizza = new Pizza.Builder(12)
                       .cheese(true)
                       .pepperoni(true)
                       .bacon(true)
                       .build();

This results in code that is easy to write and very easy to read and understand. In this example, the build method could be modified to check parameters after they have been copied from the builder to the Pizza object and throw an IllegalStateException if an invalid parameter value has been supplied. This pattern is flexible and it is easy to add more parameters to it in the future. It is really only useful if you are going to have more than 4 or 5 parameters for a constructor. That said, it might be worthwhile in the first place if you suspect you may be adding more parameters in the future.

I have borrowed heavily on this topic from the book Effective Java, 2nd Edition by Joshua Bloch. To learn more about this pattern and other effective Java practices I highly recommend it.

2 of 13
352

Consider a restaurant. The creation of "today's meal" is a factory pattern, because you tell the kitchen "get me today's meal" and the kitchen (factory) decides what object to generate, based on hidden criteria.

The builder appears if you order a custom pizza. In this case, the waiter tells the chef (builder) "I need a pizza; add cheese, onions and bacon to it!" Thus, the builder exposes the attributes the generated object should have, but hides how to set them.

🌐
Medium
serpro69.medium.com › the-builder-pattern-in-java-2c03fc6ccd16
The Builder pattern in Java — yet another Builder Pattern article? | by Serhii Prodan | Medium
April 1, 2019 - Overall, the Builder pattern is a very commonly used technique for creating objects and is a good choice to use when classes have constructors with multiple parameters (especially optional ones) and are likely to change in the future. The code becomes much easier to write and read as compared to overridden constructors, and the class scales just as well as JavaBeans but is much safer.
🌐
LangChain4j
glaforge.dev › posts › 2024 › 01 › 16 › java-functional-builder-approach
Functional builder approach in Java | Guillaume Laforge
January 16, 2024 - Learn how a functional builder approach, inspired by Golang, can simplify verbose Java object creation and deeply nested builder patterns.
🌐
Refactoring.Guru
refactoring.guru › home › design patterns › builder › java
Builder in Java / Design Patterns
January 1, 2026 - Builder pattern in Java. Full code example in Java with detailed comments and explanation. Builder is a creational design pattern, which allows constructing complex objects step by step.
Find elsewhere
Top answer
1 of 9
34

It's so you can be immutable AND simulate named parameters at the same time.

Person p = personBuilder
    .name("Arthur Dent")
    .age(42)
    .build()
;

That keeps your mitts off the person until it's state is set and, once set, won't let you change it. Yet every field is clearly labeled. You can't do this with just one class in Java.

It looks like you're talking about Josh Blochs Builder Pattern. This should not be confused with the Gang of Four Builder Pattern. These are different beasts. They both solve construction problems, but in fairly different ways.

Of course you can construct your object without using another class. But then you have to choose. You lose either the ability to simulate named parameters in languages that don't have them (like Java) or you lose the ability to remain immutable throughout the objects lifetime.

Immutable example, has no names for parameters

Person p = new Person("Arthur Dent", 42);

Here you're building everything with a single simple constructor. This will let you stay immutable but you loose the simulation of named parameters. That gets hard to read with many parameters. Computers don't care but it's hard on the humans.

Simulated named parameter example with traditional setters. Not immutable.

Person p = new Person();
p.name("Arthur Dent");
p.age(42);

Here you're building everything with setters and are simulating named parameters but you're no longer immutable. Each use of a setter changes object state.

So what you get by adding the class is you can do both.

Validation can be performed in the build() if a runtime error for a missing age field is enough for you. You can upgrade that and enforce that age() is called with a compiler error. Just not with the Josh Bloch builder pattern.

For that you need an internal Domain Specific Language (iDSL).

This lets you demand that they call age() and name() before calling build(). But you can't do it just by returning this each time. Each thing that returns returns a different thing that forces you to call the next thing.

Use might look like this:

Person p = personBuilder
    .name("Arthur Dent")
    .age(42)
    .build()
;

But this:

Person p = personBuilder
    .age(42)
    .build()
;

causes a compiler error because age() is only valid to call on the type returned by name().

These iDSLs are extremely powerful (JOOQ or Java8 Streams for example) and are very nice to use, especially if you use an IDE with code completion, but they are a fair bit of work to set up. I'd recommend saving them for things that will have a fair bit of source code written against them.

2 of 9
59

Why use/provide a builder class:

  • To make immutable objects — the benefit you've identified already.  Useful if the construction takes multiple steps.  FWIW, immutability should be seen a significant tool in our quest to write maintainable and bug free programs.
  • If the runtime representation of the final (possibly immutable) object is optimized for reading and/or space usage, but not for update.  String and StringBuilder are good examples here.  Repeatedly concatenating strings is not very efficient, so the StringBuilder uses a different internal representation that is good for appending — but not as good on space usage, and not as good for reading and using as the regular String class.
  • To clearly separate constructed objects from objects under construction.  This approach requires a clear transition from under-construction to constructed.  For the consumer, there is no way to confuse an under-construction object with a constructed object: the type system will enforce this.  That means sometimes we can use this approach to "fall into the pit of success", as it were, and, when making abstraction for others (or ourselves) to use (like an API or a layer), this can be a very good thing.
🌐
Medium
medium.com › @bectorhimanshu › crafting-a-flexible-web-application-implementing-the-builder-design-pattern-with-spring-boot-47f00c2b7706
How to create a Flexible Web Application: Implementing the Builder Design Pattern with Spring Boot | by bectorhimanshu | Medium
June 25, 2024 - Builder is a creational design pattern that lets you construct complex objects step by step. The pattern allows you to produce different types and representations of an object using the same construction code.
🌐
Medium
medium.com › @vinodkumarbheel61 › builder-design-pattern-in-java-a-practical-guide-8a9aaf3d51a3
Builder Design Pattern in Java: A Practical Guide | by Vinod Kumar | Medium
February 16, 2024 - The Builder design pattern is a creational pattern that is widely used in object-oriented programming to construct complex objects step by step. This pattern separates the construction of a complex object from its representation, allowing the ...
🌐
Medium
medium.com › @anjalimishraa17 › builder-pattern-in-java-9a870ce6e7d3
Builder Pattern in Java. What is the Builder Pattern? | by Anjali Mishra | Medium
December 28, 2024 - The pattern involves using a separate ... the Builder Pattern is often used to create immutable objects or objects requiring elaborate setup, such as configuration objects or domain-specific entities....
🌐
Medium
medium.com › @WhiteBatCodes › java-simplifying-user-object-construction-with-the-builder-pattern-e808c114e866
Java | Simplifying User Object Construction with the Builder Pattern | by WhiteBatCodes | Medium
June 10, 2023 - The Fluent Builder pattern provides a flexible and readable way to construct objects. It involves defining a builder class that contains setter methods for each attribute, allowing method chaining to create a fluent and expressive syntax.
🌐
Medium
medium.com › @harikakaranam2322 › builder-pattern-in-java-24304310fe0c
Builder Pattern in Java. Builder Pattern is a creational… | by Java_Tech | Medium
May 10, 2025 - Builder : Builder refers to an interface (or an abstract class), which has all the methods used to create the object(product).
🌐
DEV Community
dev.to › kylec32 › effective-java-tuesday-the-builder-pattern-2k5f
Effective Java Tuesday! The Builder Pattern! - DEV Community
August 27, 2023 - We get the safety of the telescoping constructor but also get vastly improved readability like the JavaBeans method (I would argue better than the JavaBeans even). In this pattern we create a static class internal to the object we want to create traditionally simply called Builder.
🌐
Izumi Notebook
izumi.pub › en › Java › effective-java-builder-pattern
Creating Objects with Builders in Effective Java
April 2, 2020 - 1. When Should You Use a Builder? In most cases, we can create an object with something as simple as new User(), so why would we need a builder at all?\nSuppose we have a class like this:\n1 2 3 4 5 6 7 public class User { private int id; private String name; private String phone; private String sex; private String IDCard; } If only id and name are required when creating a User, and the rest are optional, then there are usually two straightforward ways to build the object.\n
🌐
Anthony-galea
anthony-galea.com › blog › post › the-builder-pattern-in-java
The Builder Pattern in Java
January 9, 2016 - The Builder pattern is a creational design pattern which can be used to address the telescoping constructor anti-pattern.
🌐
Medium
medium.com › @thecodebean › builder-design-pattern-implementation-in-java-6adc6fd99ee0
Builder Design Pattern Java | The Code Bean | Medium
April 13, 2024 - While developing applications, we often come across scenarios where we need to create complex objects with numerous optional parameters. In such cases, the Builder Pattern comes to the rescue. This design pattern allows us to construct objects step by step, specifying only the attributes we need. In this blog post, we’ll explore how to implement the Builder Pattern in Java.
🌐
jrsoftware.org
jrsoftware.org › isinfo.php
Inno Setup
Inno Setup is an open-source installation builder for Windows applications by Jordan Russell and Martijn Laan. Since its introduction in 1997, Inno Setup has been trusted by developers and organizations of all sizes to reliably deploy software to millions of PCs worldwide · Don't forget to ...
🌐
Reddit
reddit.com › r/programming › a comprehensive guide on builder design pattern with all flavors & examples
r/programming on Reddit: A Comprehensive Guide on Builder Design Pattern with all Flavors & Examples
September 15, 2025 -

Constructing complex objects with numerous optional parameters often leads to a mess of telescoping constructors or error-prone setter methods. The Builder Pattern solves this by providing a clear, step-by-step process for creating objects, resulting in code that is more readable, maintainable, and thread-safe.

This article explores the pattern through a Custom Pizza Order analogy, demonstrating both the classic approach and the modern, fluent style using modern Java 21 compatible codes.

🌐
DigitalOcean
digitalocean.com › community › tutorials › builder-design-pattern-in-java
Builder Design Pattern in Java: Guide & Examples | DigitalOcean
August 3, 2022 - First of all you need to create a static nested class and then copy all the arguments from the outer class to the Builder class. We should follow the naming convention and if the class name is Computer then builder class should be named as ComputerBuilder. Java Builder class should have a public constructor with all the required attributes as parameters.