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."
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
