Quick revision
The short answer
Mutable beans with setters came from JavaBeans and early frameworks that could only create objects through a no-argument constructor; modern Spring, Jackson and Bean Validation bind through constructors, so that reason is gone. Immutable data removes whole classes of bugs, shared mutation, unsafe publication and broken hash keys, so I would move the organization over several years by starting at the boundaries (DTOs, events, configuration properties), keeping JPA entities mutable but encapsulated, changing templates so new code is immutable by default, and measuring progress through defect types and review findings.
What to remember
In this answer, we saw that mutable beans came from JavaBeans and frameworks that needed setters, a need that modern constructor binding has removed. Immutable data removes shared mutation, unsafe publication and broken hash keys. The plan starts at boundaries with DTOs, events and constructor-bound configuration properties, moves to domain value types, keeps JPA entities mutable but encapsulated behind business methods, changes templates so the default is immutable, and measures success through defect types and review findings rather than counts of converted classes.
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.
Mutable beans with setters came from JavaBeans and early frameworks that could only create objects through a no-argument constructor; modern Spring, Jackson and Bean Validation bind through constructors, so that reason is gone. Immutable data removes whole classes of bugs, shared mutation, unsafe publication and broken hash keys, so I would move the organization over several years by starting at the boundaries (DTOs, events, configuration properties), keeping JPA entities mutable but encapsulated, changing templates so new code is immutable by default, and measuring progress through defect types and review findings.
Why this question is asked
A codebase full of getters and setters is not wrong; it is a record of the tools available when it was written. Interviewers ask this leadership question to see whether we can change a habit across many teams without a rewrite, and whether we can explain why the change is worth the effort to people who have shipped working software with setters for fifteen years. In this answer, we will look at where mutable beans came from, what immutability actually fixes, then walk through the plan in phases, the places where mutability stays, and how to know the change is working.
1Where Mutable Beans Came From
The JavaBeans specification of the late 1990s defined a bean as a class with a public no-argument constructor and a getter and setter per property. GUI builders, JSP, early Hibernate, early Spring XML configuration and many JSON and XML libraries were built on that model: create an empty object by reflection, then call setters one by one. The convention spread far beyond the tools that needed it, until a class without setters looked unusual.
Today the frameworks have moved. Spring has preferred constructor injection for years. Jackson binds to constructors and, since 2.12, to records. Spring Boot binds @ConfigurationProperties through constructors, automatically for records and classes with a single constructor. Bean Validation validates records and constructor parameters. So the technical reason for setters has mostly disappeared, and what remains is habit.
2What Immutable Data Fixes
To persuade teams, it helps to be concrete about the bugs that disappear rather than arguing from style. Shared mutation is the first: one service puts an object in a cache, another changes it for its own response, and the cached copy is now wrong for everyone. Unsafe publication is the second: an object built with setters on one thread and read on another without proper synchronization may be seen half-initialized, while final fields get a guarantee from the Java memory model that they are visible once the constructor finishes. Broken hash keys are the third: an object changed while it sits in a HashMap or HashSet becomes unreachable.
Notice that the second version does not need anyone to remember a rule. The compiler simply has no setter to call. That is the argument to make: immutability turns a convention that people must remember into a property the code enforces.
3Phase One: Start at the Boundaries
The safest first step is where data crosses a boundary, because those types rarely carry behaviour and their mutation is almost always accidental. Request and response DTOs become records. Messages and events become records, which also suits them because an event is a fact that should never change. Configuration properties move to constructor binding, so a misconfigured value fails at startup and nothing can change settings at run time.
- Year 1: boundariesDTOs, events, configuration as records or constructor-bound classes
- Year 1: defaultstemplates, starters and code generators produce immutable types
- Year 2: domain valuesMoney, Email, ids as value records with local checks
- Year 2-3: entitiesstay mutable for JPA, but changes only through methods
- Ongoing: measuredefect categories, review findings, share of new mutable types
5 steps in order. Play them, or step through them one at a time.
Each phase is useful on its own; nothing requires a big rewrite.
These changes are local and easy to review, and each one is a small win the team can see. I would not ask anyone to convert old classes in bulk; types are converted when they are touched for other reasons, which spreads the work over normal feature development.
4Phase Two: Domain Values and Encapsulated Entities
Next come small domain values such as Money, Email or OrderId, written as records whose compact constructors check local rules. They replace raw strings and BigDecimals in method signatures, which removes another bug class, passing a customer id where an order id was expected.
JPA entities are the honest exception. Jakarta Persistence needs a no-argument constructor, non-final classes for proxies and changeable state so it can load and track entities, so records cannot be entities and forcing immutability on them fights the framework. What we can change is how they are mutated: remove public setters, and replace them with methods that express a business operation and keep the entity valid, such as order.addLine(product, quantity) or order.cancel(reason). The entity stays mutable, but only through a few doors that enforce its rules.
5Keeping It Going and Measuring It
A multi-year change survives only if the defaults change. The project templates and internal starters should generate records for DTOs and constructor-bound configuration from day one, the code generator for OpenAPI clients should be set to produce immutable models where it can, and the style guide should say when a setter is acceptable. Architecture tests can flag new public setters in packages such as api or events.
For measurement, I would avoid counting converted classes, which rewards busywork. Better signals are the categories of production defects, such as shared mutable state, concurrency visibility issues and lost map keys, and how often reviewers raise mutation problems. If those numbers fall over a few quarters while new code is mostly immutable by default, the change is working, and those numbers are also what keeps leadership support.
6Follow-up questions
How do you handle teams that push back because setters are simpler?
Acknowledge that setters are simpler to write and show where they cost more, with a real incident from our own systems if possible. Then make the immutable way the easiest way, through templates and IDE support, so nobody is asked to do extra work to follow the guidance.
What about large objects where creating a new copy for each change is expensive?
For most DTOs and events, allocation is cheap and short-lived objects are what the garbage collector handles best. Where a large structure really changes often, keep a mutable builder or a mutable working object inside one method or one thread, and publish an immutable result.
Do immutable types work with Spring MVC form binding?
Yes. Spring MVC data binding can use constructor binding for records and classes with a single constructor, and Jackson does the same for JSON bodies. Check your Spring version's documentation for details on nested objects and constructor parameter names.
7Conclusion
In this answer, we saw that mutable beans came from JavaBeans and frameworks that needed setters, a need that modern constructor binding has removed. Immutable data removes shared mutation, unsafe publication and broken hash keys. The plan starts at boundaries with DTOs, events and constructor-bound configuration properties, moves to domain value types, keeps JPA entities mutable but encapsulated behind business methods, changes templates so the default is immutable, and measures success through defect types and review findings rather than counts of converted classes.
Open with
I would start by explaining why the codebase looks the way it does. Setters came from the JavaBeans convention and from early frameworks, like old Hibernate, JSP and XML-configured Spring, that created an empty object and then called setters.
Then, if they want more
Modern Spring, Jackson and Bean Validation all bind through constructors and support records, so that reason is gone.
Then I would make the case with bugs rather than style: immutable data removes shared mutation, where someone changes a cached object for their own use, unsafe publication, because final fields are safely visible after construction, and hash keys that break when they change inside a map. The rollout would be gradual over a few years.
First the boundaries: request and response DTOs and events become records, and configuration properties use constructor binding, which Spring Boot 3 does automatically for records. At the same time I change the templates and starters so new services produce immutable types by default. Next, small domain values like Money or OrderId as records with local validation.
JPA entities stay mutable, because JPA needs that, but I would remove public setters and expose business methods like addLine or cancel that keep the entity valid. I would not ask for bulk conversions; types change when they are touched. And I would measure through production defect categories and review findings, not the number of converted classes.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Boot 3
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
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 "Records, unmodifiable collections and value-based classes show a long trend in Java...": https://scrivio-interview-library.netlify.app/java/records/da75acb6b2/