Backend Development 7 min read

Understanding Lombok @Builder Default Value Issue and Its Solution

This article explains why Lombok's @Builder annotation ignores field default values in Java classes, demonstrates the issue with sample code, and shows how using @Builder.Default resolves the problem by generating static default methods in the compiled builder class.

Cognitive Technology Team
Cognitive Technology Team
Cognitive Technology Team
Understanding Lombok @Builder Default Value Issue and Its Solution

When using Lombok's @Builder annotation in Java, fields that have default values defined in the class are not initialized with those defaults when the builder creates an instance.

Example code shows a class Demo with fields boolean bTest1 = true and Boolean bTest2 = true . Creating an object with new DemoBuilder().build() yields a Demo instance where both fields are false/null because the defaults are ignored.

Inspecting the byte‑code‑generated class reveals that the builder simply calls the all‑args constructor without applying the field initializers.

To fix this, Lombok provides the @Builder.Default annotation. Adding it to the fields causes Lombok to generate static methods (e.g., $default$bTest1() ) that return the specified default values, and the builder's build() method uses these methods when a field has not been explicitly set.

The resulting compiled class contains the static default methods and modified builder logic that checks a “set” flag for each field, applying the defaults only when necessary. This resolves the unexpected behavior of missing default values.

Although @Builder.Default solves the initialization issue, it introduces additional complexity, which will be explored in future articles.

JavaAnnotationsLombokBuilderDefaultValue
Cognitive Technology Team
Written by

Cognitive Technology Team

Cognitive Technology Team regularly delivers the latest IT news, original content, programming tutorials and experience sharing, with daily perks awaiting you.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.