Quick revision
The short answer
We put @Builder on Customer, mark tags with @Singular so the builder gets a tag(String) method and an unset list becomes an empty immutable list, and use @Builder.Default for any field with an initialiser we want to keep, because the builder otherwise ignores field initialisers and leaves the field null or zero. Lombok does not make name required by itself; we enforce it with @NonNull, which adds a null check to the generated constructor, or with validation in a constructor that carries @Builder.
What to remember
In this answer, we wrote a Customer builder with a required name, a default country and a tags list that supports .tag("vip"). We saw that the builder passes every field to the constructor, so field initialisers are lost unless we mark them @Builder.Default, that @Singular adds tag and tags methods and turns an unset list into an empty immutable one, and that Lombok does not enforce required fields, so we added @NonNull or validated in a constructor that carries @Builder.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We put @Builder on Customer, mark tags with @Singular so the builder gets a tag(String) method and an unset list becomes an empty immutable list, and use @Builder.Default for any field with an initialiser we want to keep, because the builder otherwise ignores field initialisers and leaves the field null or zero. Lombok does not make name required by itself; we enforce it with @NonNull, which adds a null check to the generated constructor, or with validation in a constructor that carries @Builder.
Why this question is asked
Builders make object creation readable when a class has several fields, some optional, and Lombok's @Builder writes the whole builder class for us. Interviewers like this coding question because the obvious version has two traps: a default value that silently disappears and a required field that nothing enforces. In this answer, we will recap what @Builder generates, write the Customer class step by step, see why field initialisers need @Builder.Default, look at what @Singular adds, and then make name required.
1A Quick Recap of What @Builder Generates
When we put @Builder on a class, Lombok generates a static nested class CustomerBuilder with one field and one method per field of Customer, a build() method that calls an all-args constructor, and a static method Customer.builder() that returns a new builder. If the class has no constructor of its own, Lombok also generates that all-args constructor with package-private access. So Customer.builder().name("Ana").build() is just a readable way of calling new Customer("Ana", ...).
2Writing the Customer Class
Here is the class with all three requirements: a required name, a country with a default, and a tags list that defaults to empty and accepts single tags.
Now the usage. Notice the singular tag() method, generated from the plural field name, and what happens to the fields we do not set.
3Using the Builder
Each call returns the builder, so calls chain, and build() creates the object.
So the unset list became an empty list, the default country survived, and a missing name fails at build() rather than later in the code. The exception message comes from the null check Lombok added to the constructor because of @NonNull.
4Why Field Initialisers Need @Builder.Default
The builder does not create a Customer and then change it. It collects values in its own fields and passes all of them to the constructor at the end. If we never call country(...), the builder's country field is still null, and null is what it passes to the constructor, which overwrites the initialiser. The = "US" in the field declaration never takes effect for objects made by the builder.
- Was country(...) called?the builder tracks this in a flagYesUse the value we passedNoIs the field @Builder.Default?
- Is the field @Builder.Default?YesCall the default method: "US"NoPass null to the constructor
- Call the constructorevery field gets the value chosen above
3 steps in order. Play them, or step through them one at a time.
Without @Builder.Default, an unset field is null, 0 or false.
Lombok prints a warning for exactly this case. With @Builder.Default, Lombok moves the initialiser into a static method and the builder calls it when the value was not set, so the default comes back. Notice it applies to any initialiser: numbers, empty collections, Instant.now() and so on.
5What @Singular Adds
@Singular on a collection field changes how the builder handles it. Instead of one tags(List) method that replaces the list, the builder gets tag(String) to add one element, tags(Collection) to add many, and clearTags() to start over. Lombok works out the singular name from English plurals; for a name it cannot work out, we write @Singular("tag").
At build() time, Lombok creates an immutable list from what was added, and if nothing was added, an empty immutable list. That is why tags never needs @Builder.Default: an empty list is already the default. In fact, Lombok does not allow both annotations on the same field.
6Making name Really Required
Lombok has no notion of a required builder field at compile time: Customer.builder().build() compiles. The simplest runtime check is @NonNull on the field, as above. For richer rules, such as a name that must not be blank, we put @Builder on a constructor we write ourselves and validate there.
For truly mandatory arguments, a static factory such as Customer.named("Ana") that returns a builder with the name already set makes the requirement visible in the API itself.
7Follow-up questions
How would you add validation to the generated build()?
The cleanest way is the one shown: put @Builder on a private constructor and validate in the constructor, because build() always calls it, and so does any other way of creating the object. We can also declare the builder class ourselves, public static class CustomerBuilder, and add methods Lombok then leaves alone, but overriding build() means rewriting what Lombok would generate, including the @Singular list handling, so it is more fragile. Bean Validation annotations checked by a validator are a good extra layer at API boundaries, but they do not stop invalid objects being built.
Does @Builder make the object immutable?
Not by itself. Immutability comes from final fields and no setters. Here the fields are final and we used @Getter, not @Setter or @Data, so the object cannot change after build().
What does toBuilder = true add?
@Builder(toBuilder = true) generates an instance method toBuilder() that returns a builder pre-filled with the object's values, which is a convenient way to make a modified copy of an immutable object.
8Conclusion
In this answer, we wrote a Customer builder with a required name, a default country and a tags list that supports .tag("vip"). We saw that the builder passes every field to the constructor, so field initialisers are lost unless we mark them @Builder.Default, that @Singular adds tag and tags methods and turns an unset list into an empty immutable one, and that Lombok does not enforce required fields, so we added @NonNull or validated in a constructor that carries @Builder.
Open with
I would annotate the class with @Builder and @Getter, make the fields final, put @NonNull on name, @Builder.Default on country with the value US, and @Singular on the tags list.
Then, if they want more
Then usage is Customer.builder().name("Ana").tag("vip").tag("beta").build(). @Singular gives me tag for one element, tags for a collection and clearTags, and builds an immutable list, so if I never call tag I get an empty list, not null.
The reason initialisers need @Builder.Default is how the builder works: it does not create the object and then modify it, it collects values in its own fields and passes all of them to the all-args constructor in build. A field I never set is null in the builder, so null is passed and the initialiser is overwritten.
Lombok even warns about it. @Builder.Default moves the initialiser into a static method that the builder calls when the value was not set. You cannot combine it with @Singular, which already defaults to empty. For the required name, Lombok has no compile-time required fields. @NonNull makes the generated constructor throw a NullPointerException with a clear message.
For more validation, like rejecting blank names, I put @Builder on a private constructor and validate there, because every build goes through it.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Annotate a class Customer with @Builder so that name is required, tags defaults to an...": https://scrivio-interview-library.netlify.app/java/lombok/4c79fd8979/