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.
🌐
GeeksforGeeks
geeksforgeeks.org › java › builder-pattern-in-java
Builder Method Design Pattern in Java - GeeksforGeeks
July 11, 2025 - Implementation : In Builder pattern, we have a inner static class named Builder inside our Server class with instance fields for that class and also have a factory method to return an new instance of Builder class on every invocation.
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.
🌐
Refactoring.Guru
refactoring.guru › home › design patterns › builder › java
Builder in Java / Design Patterns
January 1, 2026 - java.nio.ByteBuffer#put() (also in CharBuffer, ShortBuffer, IntBuffer, LongBuffer, FloatBuffer and DoubleBuffer) ... Identification: The Builder pattern can be recognized in a class, which has a single creation method and several methods to configure the resulting object.
🌐
DigitalOcean
digitalocean.com › community › tutorials › builder-design-pattern-in-java
Builder Design Pattern in Java: Guide & Examples | DigitalOcean
August 3, 2022 - Java Builder class should have methods to set the optional parameters and it should return the same Builder object after setting the optional attribute. The final step is to provide a build() method in the builder class that will return the Object needed by client program. For this we need to have a private constructor in the Class with Builder class as argument. Here is the sample builder pattern example code where we have a Computer class and ComputerBuilder class to build it.
🌐
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.

Find elsewhere
🌐
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.
🌐
Saigon Technology
saigontechnology.com › home › blog › [design pattern] lesson 05: builder design pattern in java
[Design Pattern] Lesson 05: Builder Design Pattern in Java - Saigon Technology
May 27, 2025 - The first strength of the Builder pattern is that it helps us control creating objects. They can have many attributes. And, they it is easy. It eliminates constructors with many parameters and reduces the number of redundant constructors. Builders can assist in creating immutable objects. StringBuilder and StringBuffer are among the many commonly used Builders provided by Java.
🌐
Medium
medium.com › @thecodebean › builder-design-pattern-implementation-in-java-6adc6fd99ee0
Builder Design Pattern Java | The Code Bean | Medium
April 13, 2024 - The Builder Pattern involves creating a separate builder class responsible for constructing the object.
🌐
Atlantbh
atlantbh.com › home › builder pattern in java
Builder Pattern in Java - Atlantbh
April 28, 2026 - Builder pattern is one of the most popular and used design patterns in Java. It is used to create instances of very complex objects, for which the usage of constructors would result in very messy and difficult to maintain code.
🌐
Medium
medium.com › @alxkm › builder-pattern-variations-and-best-practices-643b6631341f
Builder Pattern Variations and Best Practices | by Alex Klimenko | Medium
August 4, 2025 - In essence, the builder pattern provides flexibility during object construction by allowing for the setting of individual fields in a fluent manner, while still ensuring immutability once the object is fully constructed.
🌐
Vogella
vogella.com › tutorials › DesignPatternBuilder › article.html
Implementing the builder pattern in Java - Tutorial
February 26, 2026 - Typically, the Builder Pattern is implemented by a class that contains several methods for configuring the product. These methods usually return the builder object itself, enabling a fluent API, where methods can be chained together in a readable, sequential flow.
🌐
TutorialsPoint
tutorialspoint.com › design_pattern › builder_pattern.htm
Design Patterns - Builder Pattern
BuiderPatternDemo uses MealBuider to demonstrate builder pattern. package com.tutorialspoint; import java.util.ArrayList; import java.util.List; public class BuilderPatternDemo { public static void main(String[] args) { MealBuilder mealBuilder = new MealBuilder(); Meal vegMeal = mealBuilder.prepareVegMeal(); System.out.println("Veg Meal"); vegMeal.showItems(); System.out.println("Total Cost: " + vegMeal.getCost()); Meal nonVegMeal = mealBuilder.prepareNonVegMeal(); System.out.println("\n\nNon-Veg Meal"); nonVegMeal.showItems(); System.out.println("Total Cost: " + nonVegMeal.getCost()); } } int
🌐
Java Design Patterns
java-design-patterns.com › patterns › step-builder
Step Builder Pattern in Java: Crafting Fluent Interfaces for Complex Object Construction | Java Design Patterns
The Step Builder pattern in Java is an extension of the Builder pattern that guides the user through the creation of an object in a step-by-step manner.
🌐
Medium
medium.com › @spacolino › builder-pattern-in-java-a-practical-guide-6bd1784b49a4
Builder Pattern in Java: A Practical Guide | by Sašo Špacapan | Medium
December 17, 2024 - Builder Pattern in Java: A Practical Guide The Builder Pattern is a creational design pattern that simplifies the construction of complex objects. It decouples the process of building an object from …
🌐
Java Design Patterns
java-design-patterns.com › patterns › builder
Builder Pattern in Java: Crafting Custom Objects with Clarity | Java Design Patterns
Discover the Builder design pattern in Java, a powerful creational pattern that simplifies object construction. Learn how to separate the construction of a complex object from its representation with practical examples and use cases.
🌐
Medium
medium.com › @kethajayasandeep1254 › builder-pattern-java-design-patterns-the-ultimate-beginners-guide-760749e79938
Builder Pattern in Java – Ultimate Beginner's Guide | Medium
May 29, 2025 - This is a creational design pattern. In this guide, we’ll walk through the Builder Pattern like we’re working on a project together — from the problem, to the pain points, and finally the elegant solution.