Java / Lombok Interview questions
What is @Builder.Default used for?
Normally, a field's default field initializer (like private int retries = 3;) is silently
ignored when using @Builder, because the builder constructs the object through its own
constructor logic rather than the field's normal initialization path — without
@Builder.Default, an unset field in the builder would end up as its type's zero value
(0, null, etc.), not the initializer you wrote.
@Builder public class RetryConfig { @Builder.Default private int maxRetries = 3; // without this annotation, unset -> 0, not 3 } RetryConfig cfg = RetryConfig.builder().build(); // cfg.getMaxRetries() == 3, thanks to @Builder.Default
This is a common gotcha for developers new to Lombok's builder: field initializers you'd expect to "just
work" are silently dropped unless explicitly marked with @Builder.Default, so any field with a
meaningful default value needs this annotation to actually preserve that default when built via the builder.
More Related questions...