🌐
How to do in Java
howtodoinjava.com › home › java basics › builder pattern for java records
Builder Pattern for Java Records
August 6, 2024 - The builder pattern aims to provide a flexible solution for constructing complex data structures by separating the build process from the final representation of the data structure.
🌐
Medium
medium.com › @sskmal › java-records-6736f45a6aa7
Java Records and Builder pattern For Record | by sunimal malkakulage | Medium
August 21, 2024 - Starting with Java 16, you can ... int age, String address) {} Builder pattern is a design pattern that allows for a more flexible way of constructing objects in Java....
Discussions

Java record does not have default builder - Stack Overflow
Java came with records which are really useful and can avoid use of library like Project Lombok. But can someone please help me understand why records does not support Builder pattern by default? I More on stackoverflow.com
🌐 stackoverflow.com
Is Record feature in Java 16 an alternative to builder classes? - Stack Overflow
Well, simply put, you can't use records as a replacement for the builder pattern ... because they do different (in fact, orthogonal) things. Here's a point by point comparison of conventional Java classes implemented with the builder pattern versus record types: More on stackoverflow.com
🌐 stackoverflow.com
ANN: Record Builder – Builder and Withers for Java 16 Records

I've mentioned this here before but now that Java 16 is released I've made a new release compiled with the latest Java 16.

Java 16 introduces Records. While this version of records is fantastic, it's currently missing some important features normally found in data classes: a builder and "with"ers. This project is an annotation processor that creates:

  • a companion builder class for Java records

  • an interface that adds "with" copy methods

  • an annotation that generates a Java record from an Interface template

More on reddit.com
🌐 r/java
8
30
March 16, 2021
Records and @Builder
It does not defeat the purpose. Records are immutable data objects. How you initialize them, is up to you and using the builder pattern on big data objects makes sense as sometimes the default record constructor might be inflexible. If you don't want to use lombok (@Builder), this works great: https://github.com/Randgalt/record-builder More on reddit.com
🌐 r/javahelp
12
2
September 2, 2024
🌐
Baeldung
baeldung.com › home › java › a practical guide to recordbuilder in java
A Practical Guide to RecordBuilder in Java | Baeldung
November 12, 2025 - As a result, it provides a clean separation between construction logic and business logic, making the builder pattern a versatile tool across service layers. With RecordBuilder, we go beyond basic builder generation by offering customization features that let us adapt the builder to our domain-specific needs.
🌐
Medium
medium.com › @trivajay259 › builder-pattern-for-java-records-f62d884e9973
Builder Pattern for Java Records. The builder pattern aims to provide a… | by Ajay Kumar | Medium
March 12, 2026 - public record Team(String name, String description, String uuid, String photoPath) { public static final class Builder{ String name; String description; String uuid; String photoPath; public Builder uuid(String uuid) { this.uuid = uuid; return this; } public Builder description(String description) { this.description = description; return this; } public Builder name(String name) { this.name = name; return this; } public Builder status(String description) { this.description = description; return this; } public Team build() { return new Team(uuid, name, name, photoPath); } } }
🌐
DEV Community
dev.to › agavrilov76 › record-companion-simple-builder-pattern-for-java-records-4h9f
Record Companion: Simple Builder Pattern for Java Records - DEV Community
September 21, 2025 - // Record Companion Processor annotationProcessor 'io.github.ag-libs.record-companion:record-companion-builder:0.1.4' compileOnly 'io.github.ag-libs.record-companion:record-companion-builder:0.1.1' // ValidCheck library (required only for @ValidCheck integration) implementation 'io.github.ag-libs.validcheck:validcheck:0.9.4' // Bean Validation API (required only for @ValidCheck integration) compileOnly 'javax.validation:validation-api:2.0.1.Final' That's it. No configuration files, no complex setup. ... If you're looking for more features, record-builder is a popular and well-established alternative. It offers more advanced features like "wither" methods and interface-based record generation. Record Companion focuses on simplicity and ease of use, making it perfect if you just want straightforward builder patterns without the extra bells and whistles.
🌐
Coderanch
coderanch.com › t › 776593 › code-reviews › engineering › Java-Record-Builder-Practice
Java Record Builder Best Practice (Code Reviews forum at Coderanch)
September 16, 2023 - That doesn't mean that the builder pattern is useless. You can still provide a record type with a builder class to make it easier to construct certain complex records. And yes, I would make the builder class a nested class of the record type. Note that the term "inner class" explicitly refers to non-static nested classes. Say "nested class" instead. Honestly, I'm a little disappointed with records in Java.
🌐
GitHub
github.com › Randgalt › record-builder
GitHub - Randgalt/record-builder: Record builder generator for Java records · GitHub
Can be used instead of new NameAndAge(...) */ public static NameAndAge NameAndAge(String name, int age) { return new NameAndAge(name, age); } /** * Return a new builder with all fields set to default Java values */ public static NameAndAgeBuilder builder() { return new NameAndAgeBuilder(); } /** * Return a new builder with all fields set to the values taken from the given record instance */ public static NameAndAgeBuilder builder(NameAndAge from) { return new NameAndAgeBuilder(from.name(), from.age()); } /** * Return a "with"er for an existing record instance */ public static NameAndAgeBuilder
Starred by 921 users
Forked by 70 users
Languages   Java
Find elsewhere
🌐
Sonar
sonarsource.com › blog › builders-withers-and-records-java-s-path-to-immutability
Builders, Withers, and Records - Java’s path to immutability | Sonar
February 21, 2024 - Java has evolved its approach to immutability through three key patterns: the Builder pattern for constructing complex objects, withers (copy-with-modification methods) for creating modified copies, and Java Records for concisely declaring immutable ...
🌐
Medium
medium.com › @pravin3c › 3-ways-to-create-builder-pattern-for-java-records-441cb3bc94b3
3 Ways to Create Builder Pattern for Java Records | by Pravin Choudhary | Medium
April 5, 2024 - It allows step-by-step construction of complex objects and can be particularly useful when you have several optional parameters. Java record types being immutable, by default, the builder pattern is an excellent match for records.
🌐
Java Guides
javaguides.net › 2023 › 12 › builder-pattern-with-java-records.html
Builder Pattern with Java Records
March 17, 2024 - 2. Inside the Person record, a static Builder class is defined. 3. The Builder class has methods name, age, and email for setting properties and a build method to create a Person instance. 4. In the main method, the Builder is used to construct a Person object with specified properties. 5. The program prints the details of the created Person object, demonstrating the Builder pattern with a Java record.
🌐
GitHub
github.com › Pravin3c › Builder-Pattern-with-Java-Records
GitHub - Pravin3c/Builder-Pattern-with-Java-Records: Ways to Create Builder Pattern for Java Records · GitHub
Below are three approaches to implementing the builder pattern with Java Records. 1. Using Nested Static Class Step 1: Define the Record We start by defining an Employee record with multiple fields. public record Employee(Long id, String name, String company, Integer salary) {} Step 2: Create the Builder Class We then create a static inner builder class inside the Employee record.
Author   Pravin3c
Top answer
1 of 1
23

Records are not “Lombok-lite”

Recreating Lombok is not the purpose of Java record.

The purpose of Java record is to provide an explicit vehicle for transparently communicating shallowly-immutable data.

Technically, records are Java’s nominal tuple. Tuple means an ordered set of fields. Nominal means the fields have names, as does their container. Practically, you can think of a Java record as a struct on steroids.

See the official document for scope and purpose, JEP 395: Records.

So no Builder feature need be provided by default as that lies outside the scope of Java record. However, you can easily create your own Builder for any particular record class.

AI-generated

Actually, you need not write your own Builder implementation. Let a generative AI draft the code on your behalf. (Be sure to review carefully as these AIs are imperfect.)

For example:

write code for a Builder class for the Java record record Person ( String name , LocalDate birth ){}

Ecosia AI chat feature produces:

public record Person(String name, LocalDate birth) {
    
    public static class Builder {
        private String name;
        private LocalDate birth;

        public Builder name(String name) {
            this.name = name;
            return this;
        }

        public Builder birth(LocalDate birth) {
            this.birth = birth;
            return this;
        }

        public Person build() {
            return new Person(name, birth);
        }
    }
}

That code worked for me, copy-pasted, with no edits needed.

Usage:

Person person = new Person.Builder()
    .birth(LocalDate.of(2001, Month.JANUARY, 23))
    .name("Alice")
    .build();

person.toString() // ➡️ Person[name=Alice, birth=2001-01-23]

Withers

By the way, work is underway to add “withers”, a feature to instantiate a new record object based on the payload values of another. See JEP 468: Derived Record Creation (Preview).

Jakarta Validation

You may want to use Jakarta Validation (previously known as Bean Validation) to verify the validity of your new record object’s state.

Likewise, you might use Jakarta Validation in your Builder implementation.

IDE code generation

Your IDE may have a feature for generating code for a Builder from a given Java Record.

For example, IntelliJ has a built-in feature Replace Constructor with Builder. However that does not work for Java Record. But there is a plugin for that. The Record Builder Plugin can “automatically generate builder classes for your Java Records, JavaBeans and Interfaces, allowing you to select fields and configure the generated builder”.

RecordBuilder library

RecordBuilder is an interesting project that lets you mark a Java record with an annotation. Then RecordBuilder generates code for builders and withers.

I’ve not yet tried using this library, so I cannot vouch for it.

Top answer
1 of 2
18

Is Record feature in Java 16 is an alternative to builder classes?

Basically ... no it isn't.

Records provide immutability to an object, so does builder pattern.

That's not correct. The builder pattern can be applied to both immutable and mutable objects. It is actually about how objects are created rather than the nature of the objects themselves.

What are pros and cons of using record in place of builder?

Well, simply put, you can't use records as a replacement for the builder pattern ... because they do different (in fact, orthogonal) things. Here's a point by point comparison of conventional Java classes implemented with the builder pattern versus record types:

  • Mutability:
    • class + builder - either mutable or immutable objects can be created
    • record - immutable only
  • Validation:
    • class + builder - yes ... the builder can validate the arguments incrementally or in the build() method.
    • record - yes ... in constructors
  • Supports optional parameters:
    • class + builder - yes
    • record - no ... though you can implement overloaded constructors
  • Supports extends:
    • class + builder - yes
    • record - no ... though you can use default methods from an inherited interface.
  • Supports internal state / abstraction:
    • class + builder - yes
    • record - no
  • Less boilerplate code:
    • class + builder - yes (relative to classes implemented without a builder) and no (relative to records).
    • record - yes

The "less boilerplate" issue is nuanced. On the one hand a builder avoids the need for overloaded constructors or new calls with huge numbers of parameters. (But you need to implement the builder itself ... which is mostly boilerplate.) On the other hand a record can be implemented without any explicit methods and a simple record constructor with no body.

2 of 2
-1

The JDK Enhancement Proposal describes records as “classes that act as transparent carriers for immutable data”.

Use cases:

Reduces Boilerplate code: Historically, creating immutable objects in Java was rather painful work, records takes care of almost all of that work for us. Records also allows the class to be better focused on the business problem at hand by reducing boilerplate code. This makes it a compelling feature for implementing things like DDD-style Value Objects and Domain Events.

public record Address(String street, String postCode, String town, String country) {
}

Temporary containers of data: Records can be defined not only as stand-alone classes, but also locally inside a method. This makes them useful as temporary containers during data processing, for quickly creating ephemeral mock data in tests, etc. We will see an example of this below.

Data Validation: Records provide support for different validation. it reduces responsibilities from the developer to write such validation

@NonNull -> a field can not be null.      
@Min() -> a min value that a field can hold.  
@Max() -> a max value that a field can hold.
@GreaterThanZero -> a field can not have value less than or equal to zero.

Comparison to Builder Classes: The only advantage that a record class offers over builder classes is data validation.

🌐
JetBrains
plugins.jetbrains.com › plugin › 24331-record-builder
Record Builder Plugin for IntelliJ IDEA & Android Studio
The Record Builder Plugin for IntelliJ offers an effortless way to use the Builder Pattern in your Java code. This plugin allows you to automatically generate builder...
🌐
Medium
medium.com › @alexey1.gavrilov › record-companion-simple-builder-pattern-for-java-records-f86d62f38212
Record Companion: Simple Builder Pattern for Java Records | by Alexey Gavrilov | Medium
September 21, 2025 - Record Companion focuses on simplicity and ease of use, making it perfect if you just want straightforward builder patterns without the extra bells and whistles. Java Records gave us immutable data structures. ValidCheck gave us clean validation.
🌐
Softwaregarden
softwaregarden.dev › en › posts › new-java › records › vs-lombok-yet-again-with-builder-pattern
Java Records tortured with Lombok yet again (builder edition) – SoftwareGarden.dev
April 15, 2021 - Or just stick to JavaBeans. However, maybe there are still other ways to “exploit” Lombok with records and bring it to another level? Maybe this time it could be even somewhat useful? Short answer: no. Currently, there’s no ‘default’ or ‘standard’ builder for records.
🌐
OpenJDK
mail.openjdk.org › pipermail › core-libs-dev › 2021-May › 077957.html
Builder pattern for Java records
May 22, 2021 - If some project out there wants to have code generators for patterns that are sometimes useful for records, that’s great — but that’s not where the language should be focusing. > On May 21, 2021, at 11:37 AM, Alberto Otero Rodríguez <albest512 at hotmail.com> wrote: > > Hi, I have found this project on GitHub which creates a Builder for Java records: > https://github.com/Randgalt/record-builder > [https://opengraph.githubassets.com/a4e3a7b3c7b16b51e0854011fe4b031577bcc09919058baef412b03613295d20/Randgalt/record-builder]<https://github.com/Randgalt/record-builder> > GitHub - Randgalt/record-builder: Record builder generator for Java records<https://github.com/Randgalt/record-builder> > The target package for generation is the same as the package that contains the "Include" annotation.
🌐
DZone
dzone.com › coding › java › why builder is often an antipattern and how to replace it with fluent builder
Why Builder Is Often an Antipattern and How to Replace it With Fluent Builder
July 17, 2020 - In Java 14, such classes can be declared as records so necessary boilerplate code will be significantly reduced. Let's add a Builder.