Java / Lombok Interview questions
When should you use @Builder instead of a constructor?
Reach for @Builder once a class has several fields, especially a mix of required and optional
ones, or multiple fields of the same type where positional constructor arguments become easy to mix up (two
adjacent String parameters, for instance, where swapping them compiles fine but is wrong).
// error-prone: which String is which? new Person("Ada", "ada@example.com", "Engineering"); // explicit and self-documenting Person.builder() .name("Ada") .email("ada@example.com") .department("Engineering") .build();
A plain constructor (often via @AllArgsConstructor) remains perfectly fine for small classes
with two or three unambiguous fields. The builder earns its keep specifically as field count and ambiguity
grow, or when you want optional fields to have sensible defaults without needing a combinatorial explosion of
overloaded constructors to cover every combination of "which fields are specified."
More Related questions...