Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Put Lombok’s @Builder directly on a method when you want callers to construct the method’s return value through a fluent builder. Lombok generates builder methods for that method’s parameters, and build() invokes the method with the values supplied.

What method-level @Builder generates

Project Lombok documents that @Builder can be placed on a class, constructor, or method (Project Lombok: @Builder). On a method, the builder represents that method’s parameters—not every field of its return type.

For example:

public class OrderFactory {
    @Builder
    public static Order create(String customer, int quantity) {
        return new Order(customer, quantity);
    }
}

Lombok generates an inner builder class, normally named from the method’s return type, such as OrderBuilder. It contains a builder field for each parameter, fluent methods named after those parameters, a build() method that calls create(customer, quantity), a generated toString(), and a builder() factory in the containing class. The generated builder constructor is package-private. See Lombok’s feature documentation and Builder API reference.

Call the generated API like this:

Order order = OrderFactory.builder()
    .customer("Ada")
    .quantity(2)
    .build();

Each parameter method returns the builder, allowing calls to chain. build() returns the annotated method’s return type after invoking that method.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

When to put @Builder on a method

Method-level @Builder is useful when construction should go through a factory or another method rather than directly through a constructor. The method controls how its parameters become the returned object, while the generated builder gives callers a named, chainable way to supply those parameters.

The choice determines what the builder exposes:

  • Method-level: builder inputs come from the annotated method’s parameters, and build() invokes that method.
  • Constructor-level: builder inputs come from the constructor’s parameters, and build() invokes that constructor.
  • Class-level: Lombok builds from the class’s fields using a generated constructor, subject to Lombok’s documented behavior for existing constructors and fields (Project Lombok: @Builder).

Collections with @Singular

Annotate a collection parameter with @Singular when callers should be able to add elements individually as well as pass a collection. Lombok generates a singular element-adder and a plural collection-adder; its documentation also describes a clear operation for singular builders (Project Lombok: @Builder).

For instance, a method parameter representing a collection of line items can be marked @Singular so the builder offers an item-at-a-time API alongside the plural adder. The collection is still an input to the annotated method; @Singular changes how callers populate that input through the builder.

Defaults for method parameters

@Builder.Default is documented for fields: with a class-level builder, Lombok uses the field initializer when the builder does not set that field. For example, Lombok documents @Builder.Default private final long created = System.currentTimeMillis(); (Project Lombok: @Builder).

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

That field behavior does not automatically give arbitrary method parameters defaults. For a method-level builder, implement the default in the target method, or explicitly supply the desired value before calling the method. Do not treat @Builder.Default as a method-parameter default annotation.

Builder names, access, and collisions

By default, Lombok derives the method-builder class name from the target method’s return type, commonly ReturnTypeBuilder. Lombok provides configuration and annotation parameters for the builder class name, builder factory method, build method, setter prefix, access level, and related naming choices (Builder API reference).

Generated elements can interact with members you have already declared. If an element with a generated name exists, Lombok silently skips generating that element and injects the remaining missing pieces. Check for name collisions when customizing or predeclaring builder members; otherwise, the resulting API may differ from what you expect (Project Lombok: @Builder).

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

When toBuilder is supported

The API reference permits toBuilder on a constructor, a type, or a static method that returns an instance of the declaring type. In a supported case, Lombok creates an instance method that starts a builder populated with the existing object’s values (Builder API reference).

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A method returning an unrelated type is not eligible under that method-level rule. In the OrderFactory.create example, the method is declared in OrderFactory but returns Order; it does not meet the requirement that a static method return an instance of its declaring type.

Version milestones

Lombok’s feature documentation records these milestones for @Builder and related behavior (Project Lombok: @Builder):

Feature Documented version
@Builder introduced as an experimental feature v0.12.0
Promoted to the main lombok package v1.16.0
@Singular clear support v1.16.8
@Builder.Default added v1.16.16
Empty builderMethodName accepted v1.18.8

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.