Quick revision
The short answer
Yes, immutability should be the default for domain values and data transfer objects, with documented exceptions. Immutable objects are thread-safe, easy to reason about and safe to cache and share, records and unmodifiable collections now make them cheap to write, and the allocation cost of creating new objects instead of mutating is usually small with modern garbage collectors. JPA entities and some frameworks need mutable objects, so those exceptions are written down, and the standard is introduced for new code and when touching old code, supported by review guidance and examples rather than a rewrite.
What to remember
In this answer, we argued for immutability as the default for domain values, DTOs and configuration: immutable objects are thread-safe, easy to reason about and safe to cache and share, records and unmodifiable collections make them cheap to write, and the allocation cost is usually negligible with modern collectors. JPA entities, builders and measured hot paths are documented exceptions. The standard is introduced through a decision record, examples, review questions and gradual automated checks, applied to new code and to old code when it is touched.
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.
Yes, immutability should be the default for domain values and data transfer objects, with documented exceptions. Immutable objects are thread-safe, easy to reason about and safe to cache and share, records and unmodifiable collections now make them cheap to write, and the allocation cost of creating new objects instead of mutating is usually small with modern garbage collectors. JPA entities and some frameworks need mutable objects, so those exceptions are written down, and the standard is introduced for new code and when touching old code, supported by review guidance and examples rather than a rewrite.
Background to this question
A standard that applies to every team shapes millions of lines of code for years, so a senior engineer must be able to argue it from benefits and costs, anticipate the exceptions, and roll it out without stalling delivery. Immutability is a good candidate because its benefits are large and its costs have fallen sharply with modern Java. In this answer, we will go through the benefits with concrete failure examples, look at why the cost of writing immutable code is now low, address performance honestly, list the places where mutability is required, and describe how to introduce the standard across teams.
1The Benefits, With the Bugs They Prevent
An immutable object cannot change after construction. That single property removes whole classes of bugs. It is thread-safe without locks, as the Java Memory Model guarantees correctly constructed objects with final fields are seen fully initialised by every thread. It is easy to reason about: if a method receives a Money value, no other code can change it while the method runs, so we never need to ask who else holds a reference. It is safe to cache and share: a cached product price can be handed to a thousand requests, and none of them can corrupt it for the others.
The bugs these properties prevent are familiar to anyone who has run production systems: an order total that changed because a discount calculation mutated a shared Money object, a HashSet that seems to lose an element because fields used in its hashCode were changed after insertion, a cache that returns a configuration object some earlier request modified, or one thread reading an address while another is halfway through updating it. Each of these can take days to find in production, and each is impossible when the values cannot change.
2Why the Cost of Writing It Has Fallen
Ten years ago, an immutable class in Java meant a constructor, private final fields, getters, equals, hashCode, toString and often a builder: fifty lines for a value with four fields, and the boilerplate was itself a source of bugs. Records, standard since Java 16, change that completely.
Unmodifiable collections from List.of, List.copyOf, Map.of and Stream.toList() cover the collection side. Jackson has supported records for years, Spring binds configuration properties to records, and Bean Validation annotations work on record components. So in most code, writing the immutable version is now shorter than writing the mutable one, which is the strongest argument for making it the default: the right thing is also the easy thing.
3Performance: What Creating New Objects Really Costs
The usual objection is that creating a new object for every change is slow. With modern collectors, allocation in the young generation is a pointer bump in a thread-local buffer, a few nanoseconds, and objects that die young cost almost nothing to collect, because a young collection only copies the survivors. G1, the default collector, and ZGC both handle high rates of short-lived allocation well. For typical business code, the allocation cost of immutable values does not show up in profiles; database calls and serialisation dominate.
There are honest exceptions. A tight loop that updates a large structure millions of times per second, such as a matrix in a simulation or a buffer in a parser, is better written with mutable state kept local to one method or one thread. The standard should say that mutation is fine when it is confined, meaning no other thread or component can see it, and that performance exceptions need a profile, not a guess.
4Where Mutability Is Required: The Documented Exceptions
Some frameworks are built around mutable objects, and the standard must say so up front, or teams will either fight the framework or ignore the standard. JPA entities are the main case: Hibernate and other providers need a no-argument constructor, set fields after construction, track changes by comparing entity state, and create proxy subclasses for lazy loading, so entity classes cannot be final records. The practical rule is to keep entities mutable but inside the persistence layer, and to expose immutable records from repositories or services. Hibernate can map read-only entities and supports records for embeddable values in recent versions; check what your version supports.
Other exceptions include builders, which are mutable by design but short-lived and confined; some UI or form-binding frameworks that bind to setters; and performance-critical code as described above. Each exception goes into the standard with the reason and the approved pattern, so reviewers can tell a legitimate exception from a habit.
| Kind of object | Standard | Reason |
|---|---|---|
| Domain values (Money, Address) | Immutable record | Shared freely, used as keys |
| DTOs and API payloads | Immutable record | Jackson binds records |
| Configuration properties | Immutable record | Spring binds records via constructor |
| JPA entities | Mutable, inside persistence layer | Provider needs setters, proxies, dirty checking |
| Builders, local buffers | Mutable, confined to one method or thread | Short-lived, never shared |
| Hot loops with profiles | Mutable with justification | Measured allocation cost |
5Introducing the Standard Across Teams
The rollout follows the same principle as any large-codebase change: new code first, old code when touched, no mass rewrite. A short architecture decision record states the default, the reasons and the exceptions. A guide with before-and-after examples, such as a mutable DTO turned into a record and a JPA entity exposed through a record projection, does more than any rule, because engineers copy examples. Code review guidance adds two questions: is this new class immutable, and if not, which documented exception applies?
Automated checks reinforce it gently. An ArchUnit test can require that classes in a domain or api package are records or have only final fields, and static analysis such as Error Prone can flag mutable static state. We would start these as warnings for a quarter and turn them into failures once the existing violations are either fixed or listed as known. Over time, refactoring on touch converts the code that changes most, which is the code where immutability pays back most.
- Write the decision recorddefault, reasons, documented exceptions
- Publish examplesrecord DTOs, entity-to-record projections
- Add review questionsimmutable? if not, which exception?
- Add ArchUnit checks as warningsthen failures after a quarter
- Refactor old code when touchednever as a mass rewrite
5 steps in order. Play them, or step through them one at a time.
6Follow-up questions
How do you handle objects that need many fields changed step by step?
Use a builder or withers: methods like withStatus(newStatus) that return a copy with one field changed. A builder collects values in a mutable, confined object and produces the immutable result at the end. Derived record creation, a language feature to write such copies concisely, has been proposed for Java; check whether it is available in your version.
Does immutability conflict with object-oriented design, where objects have behaviour and state?
No. Immutable objects still have behaviour, such as Money.plus, and entities with identity and lifecycle can still be mutable where the domain needs it. The standard targets values and data carriers, where mutation brings risk without benefit, and leaves room for mutable entities with clear ownership.
7Conclusion
In this answer, we argued for immutability as the default for domain values, DTOs and configuration: immutable objects are thread-safe, easy to reason about and safe to cache and share, records and unmodifiable collections make them cheap to write, and the allocation cost is usually negligible with modern collectors. JPA entities, builders and measured hot paths are documented exceptions. The standard is introduced through a decision record, examples, review questions and gradual automated checks, applied to new code and to old code when it is touched.
Open with
I would make immutability the default for values and data objects, with written exceptions. The benefits are big: immutable objects are thread-safe without locks, easy to reason about because nobody else can change them while my code runs, and safe to cache and share, and they remove bugs like a shared Money being modified by a discount calculation or a HashSet losing an element whose hash fields changed.
Then, if they want more
The main old cost was boilerplate, and records plus List.copyOf and the other unmodifiable collections have removed it, so the immutable version is now often shorter.
The performance worry is usually unfounded: young-generation allocation is a pointer bump and short-lived objects are nearly free to collect with G1 or ZGC, so I would only accept a mutable exception in a hot path with a profile to back it.
The real exceptions are frameworks that need mutability, mainly JPA entities, which need setters, proxies and dirty checking, so I keep entities mutable but inside the persistence layer and expose records from it. Builders and local buffers are fine if they are confined.
To introduce it, I would write a short decision record, publish before-and-after examples, add review questions, and add ArchUnit checks as warnings first, then failures. It applies to new code and old code when touched, never as a mass rewrite.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 16
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
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 "Should your organisation make immutability the default for domain and data objects...": https://scrivio-interview-library.netlify.app/java/concurrency-patterns/3067ea61ea/