This is how you use @Builder.

//Employee.Java
import lombok.Builder;
import lombok.ToString;

@Builder
@ToString
public class Employee {
   private final String empName;
   private final int salary;
}

//   Main.java

public class Main {

public static void main(String[] args) {
    Employee emp = Employee.builder().empName("Deendaya").salary(100).build();
    System.out.println(emp);
  }
}
Answer from Deendayal Garg on Stack Overflow
🌐
Project Lombok
projectlombok.org › features › Builder
@Builder
If using @Builder to generate builders to produce instances of your own class (this is always the case unless adding @Builder to a method that doesn't return your own type), you can use @Builder(toBuilder = true) to also generate an instance method in your class called toBuilder(); it creates a new builder that starts out with all the values of this instance. You can put the @Builder.ObtainVia annotation on the parameters (in case of a constructor or method) or fields (in case of @Builder on a type) to indicate alternative means by which the value for that field/parameter is obtained from this instance.
🌐
Baeldung
baeldung.com › home › java › using lombok’s @builder annotation
Using Lombok's @Builder Annotation | Baeldung
July 25, 2024 - Learn how the @Builder annotation in Project Lombok can help you reduce boilerplate code when implementing the builder pattern to create instances of your Java classes.
🌐
Medium
medium.com › jakarta-ee › the-builder-annotation-in-java-1fce61e5ae3d
The @Builder Annotation in Java. Writing clean and concise code is… | by Tarun Telang | Jakarta EE | Medium
March 12, 2025 - The @Builder annotation, specifically, generates a builder pattern for a class, allowing you to create instances of the class with a fluent and expressive API. Using @Builder is straightforward.
Top answer
1 of 2
40

This is how you use @Builder.

//Employee.Java
import lombok.Builder;
import lombok.ToString;

@Builder
@ToString
public class Employee {
   private final String empName;
   private final int salary;
}

//   Main.java

public class Main {

public static void main(String[] args) {
    Employee emp = Employee.builder().empName("Deendaya").salary(100).build();
    System.out.println(emp);
  }
}
2 of 2
35

Using @Builder on method to create a Dog and Cat instance.

In this example @Value creates a final immutable value object with accessor methods (getters), an all args constructor, equals(), hashCode() and toString().

import static org.junit.Assert.*;
import lombok.Builder;
import lombok.Value;

import org.junit.Test;

@SuppressWarnings("javadoc")
public class ImmutableAnimals {

    @Builder(builderMethodName = "dogBuilder")
    public static Dog newDog(String color, String barkSound) {
        return new Dog(color, barkSound);
    }

    @Builder(builderMethodName = "catBuilder")
    public static Cat newCat(String color, String meowSound) {
        return new Cat(color, meowSound);
    }

    public static interface Animal {
        String getColor();
    }

    @Value
    public static class Cat implements Animal {
        String color;
        String meowSound;
    }

    @Value
    public static class Dog implements Animal {
        String color;
        String barkSound;
    }

    @Test
    public void testDog() {
        final String expectedBarkSound = "woof";
        final String expectedColor = "brown";

        final Dog dog = ImmutableAnimals.dogBuilder()
            .barkSound(expectedBarkSound)
            .color(expectedColor)
            .build();

        assertEquals(expectedBarkSound, dog.getBarkSound());
        assertEquals(expectedColor, dog.getColor());
    }

    @Test
    public void testCat() {
        final String expectedMeowSound = "purr";
        final String expectedColor = "white";

        final Cat cat = ImmutableAnimals.catBuilder()
            .meowSound(expectedMeowSound)
            .color(expectedColor)
            .build();

        assertEquals(expectedMeowSound, cat.getMeowSound());
        assertEquals(expectedColor, cat.getColor());
    }
}

Here's another example with the same domain classes but using mutable values. However, as always favor immutability whenever possible.

import static org.junit.Assert.*;
import lombok.Builder;
import lombok.Data;

import org.junit.Test;

@SuppressWarnings("javadoc")
public class MutableAnimals {

    @Builder(builderMethodName = "dogBuilder")
    public static Dog newDog(String color, String barkSound) {
        final Dog dog = new Dog();
        dog.setBarkSound(barkSound);
        dog.setColor(color);
        return dog;
    }

    @Builder(builderMethodName = "catBuilder")
    public static Cat newCat(String color, String meowSound) {
        final Cat cat = new Cat();
        cat.setMeowSound(meowSound);
        cat.setColor(color);
        return cat;
    }

    public static interface Animal {
        String getColor();
    }

    @Data
    public static class Cat implements Animal {
        String color;
        String meowSound;
    }

    @Data
    public static class Dog implements Animal {
        String color;
        String barkSound;
    }

    @Test
    public void testDog() {
        final String expectedBarkSound = "woof";
        final String expectedColor = "brown";

        final Dog dog = MutableAnimals.dogBuilder()
            .barkSound(expectedBarkSound)
            .color(expectedColor)
            .build();

        assertEquals(expectedBarkSound, dog.getBarkSound());
        assertEquals(expectedColor, dog.getColor());
    }

    @Test
    public void testCat() {
        final String expectedMeowSound = "purr";
        final String expectedColor = "white";

        final Cat cat = MutableAnimals.catBuilder()
            .meowSound(expectedMeowSound)
            .color(expectedColor)
            .build();

        assertEquals(expectedMeowSound, cat.getMeowSound());
        assertEquals(expectedColor, cat.getColor());
    }
}
🌐
DEV Community
dev.to › umr55766 › why-should-we-use-lombok-s-builder-annotation-249n
Why should we use Lombok's @Builder annotation ? - DEV Community
July 26, 2021 - It provides getter, setter, constructors, few other default functions like equals, etc. We just have to use it’s annotations. It works out of the box. @builder annotation enforces Builder Design Pattern.
🌐
HowToDoInJava
howtodoinjava.com › home › lombok › lombok @builder
Lombok @Builder with Examples- HowToDoInJava
December 15, 2021 - Lombok’s @Builder annotation is a useful technique to implement the builder pattern that aims to reduce the boilerplate code. In this tutorial, we will learn to apply @Builder to a class and other useful features.
🌐
Baeldung
baeldung.com › home › java › java annotation processing and creating a builder
Java Annotation Processing and Creating a Builder | Baeldung
March 27, 2025 - The annotation processing API is located in the javax.annotation.processing package. The main interface that you’ll have to implement is the Processor interface, which has a partial implementation in the form of AbstractProcessor class. This class is the one we’re going to extend to create our own annotation processor. To demonstrate the possibilities of annotation processing, we will develop a simple processor for generating fluent object builders for annotated classes.
🌐
GitHub
github.com › AliAsadi › builder-annotation
GitHub - AliAsadi/builder-annotation: ⏳ An annotation processor which implements "Builder Pattern" for your java classes.
we annotated our class with @Builder and initialize a constructor with all the fields. @Builder public class User { private int id; private String name; private String pass; private String email; private String age; public User(int id, String name, String pass, String email, String age) { this.id = id; this.name = name; this.pass = pass; this.email = email; this.age = age; } }
Author   AliAsadi
Find elsewhere
🌐
GitHub
github.com › skinny85 › jilt
GitHub - skinny85/jilt: Java annotation processor library for auto-generating Builder (including Staged Builder) pattern classes · GitHub
Java annotation processor library for auto-generating Builder (including Staged Builder) pattern classes - skinny85/jilt
Starred by 312 users
Forked by 21 users
Languages   Java
🌐
Java By Examples
javabyexamples.com › delombok-builder
Home | Java By Examples
When we want to create a builder for specific fields, we should create a constructor with only those fields. Then when we put the @Builder annotation on the constructor, Lombok creates a builder class containing only the constructor parameters.
🌐
Project Lombok
projectlombok.org › api › lombok › Builder
Builder (Lombok)
Instances of TBuilder are made with the method named builder() which is also generated for you in the class itself (not in the builder class). The TBuilder class contains 1 method for each parameter of the annotated constructor / method (each field, when annotating a class), which returns the builder itself.
🌐
Java Guides
javaguides.net › 2019 › 03 › project-lombok-builder-pattern-using-builder-annotation.html
@Builder Lombok Annotation Example
August 11, 2023 - When you annotate a class, constructor, or method with @Builder, Lombok will automatically create an inner static Builder class with methods for setting properties and a build() method to create an instance of the containing class.
🌐
iO Flood
ioflood.com › blog › lombok-builder
Lombok @Builder | Streamlining Java Object Creation
March 5, 2024 - In this example, we’ve used the @Builder annotation on the User class. This enables us to use the builder() method to create a new User object. We’ve set the name to ‘John’ and the age to 30 using the builder pattern. The build() method then constructs and returns the User object. This is a basic way to use the Lombok builder pattern in Java...
🌐
Medium
medium.com › @sridharnarayanmkr107 › spring-boot-builder-d0edc5595cda
Spring Boot @Builder | by Sridhar Narayanasamy | Medium
April 23, 2023 - The @Builder annotation is a feature in Lombok, a library that reduces boilerplate code in Java classes. It is commonly used in Spring Boot applications to generate builder methods for classes.
🌐
Devwithus
devwithus.com › lombok-builder-annotation
How to Use Lombok Builder Annotation | devwithus.com
November 12, 2020 - This tutorial will help you understand how to use Lombok builder annotation to implement the builder design pattern in Java and reduce the boilerplate code.
Top answer
1 of 2
7

As per @Builder docs this annotation can work together with @NonNull. If the field marked @NonNull is null you will get an NullPointerException preventing creation of invalid object:

@Builder
static class Person {
  @NonNull
  private final String name;
  @NonNull
  private final Integer age;
}

public static void main(String[] args) {
  Person.builder()
        .name("Fred")
        .build(); // java.lang.NullPointerException: age is marked @NonNull but is null
}

To go further you can define the builder method yourself. If the method is present Lombok won't generate it and you can now force the arguments compile time.

@Builder
static class Person {
  @NonNull
  private final String name;
  @NonNull
  private final Integer age;

  public static PersonBuilder builder(String name, Integer age) {
    return new PersonBuilder().name(name).age(age);
  }
}

public static void main(String[] args) {
  Person.builder("Fred", 11)
        .build();
}

However one could still create a builder by writing new Person.PersonBuilder() because the builder class is still accessible.

2 of 2
1

Also, in extension can be used:

@Builder.Default <modifier><instanceVariable>=<default-value>,

like: @Builder.Default private String myVariable = "".

Please read this: @Builder default properties

Avoids compiler errors asking for required properties that won't be set in some scenarios such as persistence, indexing, etc. (update/deleteBy/findBy from id or reduced properties set, without having a full object graph).

It's an elegant solution for not overriding .build() or setters as suggested.

🌐
Baeldung
baeldung.com › home › java › lombok @builder with inheritance
Lombok @Builder with Inheritance | Baeldung
May 11, 2024 - >> Learn Java Basics · The Lombok library provides a great way to implement the Builder Pattern without writing any boilerplate code: the @Builder annotation. In this short tutorial, we’re specifically going to learn how to deal with the @Builder annotation when inheritance is involved.
🌐
Medium
medium.com › @drivaspedro › java-lombok-builder-annotation-with-inheritance-6541087040f2
Java Lombok Builder Annotation With Inheritance | by Drivaspedro | Medium
December 23, 2022 - If you need to use a previous release, you can change the default builder method name on the child class, and use the annotation on its constructor. @Builder @AllArgsConstructor public class Person { private String name; } public class Developer extends Person { private String language; @Builder(builderMethodName = "developerBuilder") public Developer(String name, String language) { super(name); this.language = language; } } // Then instantiate the class like this: Developer developer = Developer .developerBuilder() .name("Drivas") .language("java") .build();
🌐
LinkedIn
linkedin.com › learning › learning-lombok-streamlined-java-programming › lombok-implements-a-builder-design-pattern
Lombok implements a builder design pattern - Spring Video Tutorial | LinkedIn Learning, formerly Lynda.com
April 11, 2023 - We'll add the annotation, @builder.default. What this annotation will do is force this field to be defaulted to the full-time employee when we use the builder. The create date is another place where we could use this, but we'll save that for the challenge exercises later on.