Quick revision
The short answer
Both proposals give up something real. Private fields without setters are not enough, because without final an object published through a data race may be seen with default field values, while final fields carry a Java Memory Model guarantee; final also documents intent and makes the compiler reject accidental reassignment, and records give us final fields and a canonical constructor for value classes. Setting final fields by reflection after construction is not covered by that guarantee, and the JDK is moving to restrict such mutation, so check your JDK's behaviour and deserialize through a constructor or factory instead.
What to remember
In this answer, we saw that private fields without setters are immutable only by convention, and that without final an object published through a data race can be seen with default values, while final fields carry a memory-model guarantee, document intent and let the compiler stop reassignment. Records package this for value classes. Setting final fields by reflection after construction falls outside the guarantee, is refused for records and is being restricted by the JDK, so deserialization should go through a constructor or factory.
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.
Both proposals give up something real. Private fields without setters are not enough, because without final an object published through a data race may be seen with default field values, while final fields carry a Java Memory Model guarantee; final also documents intent and makes the compiler reject accidental reassignment, and records give us final fields and a canonical constructor for value classes. Setting final fields by reflection after construction is not covered by that guarantee, and the JDK is moving to restrict such mutation, so check your JDK's behaviour and deserialize through a constructor or factory instead.
Why this question is asked
Code reviews often turn on small keywords, and final is one where people disagree. One view is that if nothing can change a field, final is just noise. Another is that frameworks need to set fields after construction, so final is in the way. Interviewers give us both proposals together to see whether we can explain, calmly and concretely, what final adds in concurrent code, and how to get deserialization working without fighting the language. In this answer, we will look at the first proposal and what it loses, then at what final gives beyond the memory model, then at the reflection proposal and its problems, and finally at how to deserialize into immutable value classes properly.
1Proposal 1: Private Fields, No Setters, No final
The teammate's class looks immutable from outside: nobody can call a setter, and the fields are private. Within one thread, that is true. The problem appears when an instance is shared between threads without safe publication, for example stored in a plain field of a cache and read by request threads.
The Java Memory Model says a thread is only guaranteed to see another thread's write if a happens-before edge connects them. A plain field publication creates no edge, so the reader may see the reference before the constructor's writes. With final fields, there is a separate guarantee: for a properly constructed object, one whose this did not escape during construction, every thread that obtains a reference sees the final fields as the constructor set them, even through a data race. So final turns an object that is immutable by convention into one that is safe to share by rule.
2What final Adds Beyond the Memory Model
The memory model argument is the strongest, but not the only one. final documents intent: a reader of the class sees immediately that the value never changes, without reading every method. The compiler enforces it: a later change that adds a method assigning the field, or a constructor path that forgets to assign it, fails to compile instead of producing a subtle bug. And because no code can reassign the field, tools and the JIT can rely on it more.
For a value class like Money, a record is the simplest way to get all of this.
| Property | Private, no setters | final fields | Record |
|---|---|---|---|
| Safe under racy publication | No | Yes | Yes |
| Compiler stops reassignment | No | Yes | Yes |
| Intent visible in the code | Partly | Yes | Yes |
equals, hashCode, toString | Hand-written | Hand-written | Generated |
| Single construction path | Not enforced | Not enforced | Canonical constructor |
A record's fields are private and final, it has a canonical constructor that every instance goes through, and it gets equals, hashCode and toString from its components. That is exactly the shape of a value class, and validation lives in one place.
3Proposal 2: Setting final Fields by Reflection
The second teammate wants to keep fields final but let a deserializer set them after construction, using reflection: create the object with a no-argument constructor, then call Field.setAccessible(true) and Field.set() on each final field. Some older libraries did exactly this.
There are three problems. First, the final field guarantee covers values written in the constructor, before what the specification calls the freeze at the end of construction. A value written later by reflection is an ordinary write with no happens-before edge to other threads, so the safety we wanted from final is lost for exactly the values that matter. Second, the JIT may treat some final fields as constants and keep using the old value, so the change might not even be seen by the same code later. Third, the platform is closing this door. Reflection already refuses to modify final fields of records and hidden classes, and the JDK is moving to restrict changing final fields through reflection more generally, with warnings and options to control it; check your JDK's behaviour, because the details depend on the release.
4Deserializing Through a Constructor or Factory
The good news is that modern frameworks do not need reflection on final fields. They can call a constructor or factory with the parsed values, which keeps every instance going through the same validated path and keeps the final field guarantee intact.
With this, the deserialized object is built exactly like any other instance: the constructor validates, the fields are final, and the object is safe to share. For formats or libraries that cannot call constructors, a small mapping layer that reads into a mutable DTO and then builds the immutable value through a factory keeps the value class clean.
5Follow-up questions
If we always publish objects safely, does final still matter?
Less for visibility, since safe publication through volatile, a lock or a concurrent collection makes the whole state visible. But we rarely control every path an object travels, so final is cheap insurance. And the compiler checks and documentation benefits stay regardless of how objects are published.
Does final make the class immutable?
Not on its own. A final field pointing to a mutable list can still have its contents changed. For a value class we also copy mutable inputs, such as List.copyOf in the constructor, and avoid exposing mutable internals.
What would you say if a framework you depend on requires a no-argument constructor and setters?
I would keep that framework's type at the edge, as a DTO, and convert it to the immutable value class at the boundary. Many frameworks, including JPA entities, have such requirements, and they are a reason to separate entity or DTO types from the value objects shared across threads.
6Conclusion
In this answer, we saw that private fields without setters are immutable only by convention, and that without final an object published through a data race can be seen with default values, while final fields carry a memory-model guarantee, document intent and let the compiler stop reassignment. Records package this for value classes. Setting final fields by reflection after construction falls outside the guarantee, is refused for records and is being restricted by the JDK, so deserialization should go through a constructor or factory.
Open with
To the first teammate I would say that private fields without setters make the class immutable by convention, but final adds a real guarantee. Under the Java Memory Model, if an instance is published through a data race, say a plain field in a cache, another thread can see the reference before the constructor's writes and read null or zero from non-final fields.
Then, if they want more
Final fields are covered by the final field guarantee: for a properly constructed object, any thread that obtains the reference sees the values set in the constructor. Final also documents intent and lets the compiler reject accidental reassignment or a constructor that forgets a field.
For a value class, I would suggest a record, which gives private final fields, a canonical constructor for validation, and equals, hashCode and toString. To the second teammate I would say that setting final fields by reflection after construction defeats the purpose.
Those writes happen after the freeze at the end of the constructor, so the final field guarantee does not cover them and other threads may not see them; the JIT may even keep using the old value.
Reflection already refuses to modify record fields, and the JDK is moving to restrict changing final fields through reflection more broadly, so I would check our JDK's behaviour rather than rely on it.
The better approach is to deserialize through a constructor or factory: Jackson supports records directly and regular classes through @JsonCreator, and Java serialization uses the canonical constructor for records. If a framework insists on setters, I would keep a DTO at the edge and convert it to the immutable value.
About this answer
- Last updated
- 12 November 2026
- 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
- JLS chapter 17, Threads and Locks
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 "A teammate proposes making a value class immutable by making its fields private and not...": https://scrivio-interview-library.netlify.app/java/java-memory-model/e1adce8704/