Lesson · Java Memory Model (JMM)
Final-Field Semantics: the constructor's guarantee
14 min readFinal-Field Semantics
Final fields come with a construction-time freeze: an object published without any synchronization still shows its final field values, because constructor writes cannot be reordered past the freeze and reads of final fields cannot be reordered before the reference. The guarantee has one condition, and it is the one people break: the object must not escape while it is being constructed.
The object that arrives half-built
Two threads share a reference with no synchronization at all: one constructs a holder and stores it in a plain static field, the other reads that field and dereferences it. This is unsafe publication, and for ordinary fields it can hand the reader the object before its values exist.
The classic symptom is a holder whose field still reads 0 after the reference is non-null. Nothing failed from the writer's point of view: the reader simply observed the writes in a different order than the writer performed them, because no synchronization edge ties the two threads together.
class Holder { int value; // plain field: no construction-time guarantee Holder(int value) { this.value = value; // write #2 } } class UnsafePublish { static Holder shared; // plain static field: no publication fence public static void main(String[] args) { new Thread(() -> shared = new Holder(42)).start(); // write #1: the reference System.out.println(shared != null ? shared.value : -1); // can print 0 } }
Prediction: which write becomes visible first? Nothing in the program says. The reference and the field are two plain variables, and the reader has no guarantee that it sees them in the writer's order. The fix needs no lock, only one keyword in the right place.
The freeze at the end of the constructor
The memory model gives final fields a rule of their own (JLS 17.5). Construction ends with a freeze action: writes the constructor performed to final fields stay before the freeze, and a later read of a final field cannot be reordered before the dereference of the reference that reaches the object.
The effect is the guarantee people rely on without naming it. A thread that reads a reference to an object and then reads that object's final fields is guaranteed to see the values the constructor assigned, even when the reference arrived with no synchronization at all.
The rule is precise about its scope, and the precision matters: it covers final fields written during construction. Ordinary fields have no freeze, and code that runs after the constructor (or rewrites the field outside it) has no protection from this rule. The condition attached to the guarantee is that no other thread gets to see the object before its constructor finishes.
| This | Is ordered by the freeze |
|---|---|
| writes to final fields in the constructor | they cannot be reordered after the freeze at the end of construction |
| reads of final fields | they cannot be reordered before the dereference of the reference that reaches the object |
| writes and reads of non-final fields | nothing: unsafe publication can still show defaults |
Final fields carry a construction-time freeze; ordinary fields do not. Once the freeze has happened, final values are safe to read; before it, nothing is promised. Every escape rule in the next section is that same sentence wearing a disguise.
The one condition: no escape
The guarantee has a condition, and it is easy to break by accident: while the constructor runs, no code may let another thread see the object. In the wording of the specification, do not write a reference to the object being constructed in a place where another thread can see it before the constructor finishes.
An escape lets a reader observe the object before the freeze, and observations from that window can show final fields as their defaults, exactly as if the fields were plain. The compiler will not warn, and the object looks perfectly ordinary.
- Registering a listener: this handed to a listener list, event bus or callback registry inside the constructor. The registry publishes the object while it is still under construction.
- Starting a thread from the constructor: the new thread receives this through the closure or the Runnable, and it can run before the constructor returns.
- Calling an overridable method: the method runs on this, and a subclass override can hand the reference to anything it likes. The base class cannot know what the override does.
- Storing this in a static field or a shared map: the direct form of publication, done one line too early.
The constructor is a promise: nothing sees the object before it is built. The window closes when the constructor returns, so the fix is almost always to register, subscribe or publish on the outside, after construction.
JLS 17.5 · freeze and publication
final fields show constructor values through an unsynchronized referencethe guarantee · no escape
constructor writes final value = 42
freeze · end of constructor
constructor writes cannot move after it
reference published (plain field)
no synchronization anywhere
reader dereferences, reads value
cannot move before the dereference
Result: the reader sees 42. The freeze did the work that a lock would otherwise have to do, for the final fields only.
the escape · the guarantee is void
constructor: register(this)
this is now visible to another thread
listener runs before the freeze
the constructor is still on the stack
listener reads value
observes 0, the default
the freeze arrives later, after the observation
Construct first, publish second. The freeze orders the final-field writes before any later read of the object, but it can only do that if nobody looked before construction finished. Registering a listener, starting a thread, or calling an overridable method inside the constructor all invite the early look.
The guarantee, three ways
The player runs one program in three scenes: a plain field under unsafe publication, a final field with no escape, and a final field whose constructor registers a listener. Steps move between the constructing thread and the reading thread.
The plain-field scene shows one permitted outcome, because nothing promises anything there. The final-field scenes show what the freeze makes certain, and the third one shows the condition failing.
Same program, one keyword. The reference arrives the same way in all three scenes; what changes is what the reader is promised about the object behind it.
final-field lab · the freeze decides
holder.value
0 (default)
about to construct
not started
Scene 1: a holder with one plain int field, published through an ordinary static reference. The field starts at its default 0, and there is no final field here to rely on.
A non-null reference does not mean the object is published: the freeze marker shows whether construction finished before the reader looked. Scene 1 shows one permitted outcome, scene 2 the guarantee, scene 3 the escape that removes it.
Safe publication, the short catalogue
The freeze covers final fields and nothing else, so the general question stays: how do you hand an object to another thread so that everything about it is visible? The catalogue is short, and every entry is a named happens-before edge rather than a trick.
- Static initialiser: class initialization happens-before any use of the class, so objects built there are safely published by construction.
- A final-field object behind a safely published reference: once the reference crosses one of these edges, the freeze guarantees the final state behind it.
- A volatile reference: the write is a release and the read an acquire, so assigning the object to a volatile field carries every earlier write with it.
- Synchronized on both sides: write the reference inside a synchronized block and read it inside a synchronized block on the same monitor.
- A concurrent collection: putting the object into a thread-safe collection publishes it, because the collection supplies the internal synchronization.
- Thread.start and join: everything the starting thread did before start() happens-before the first action of the new thread, and everything a thread did happens-before another thread's join returns.
One line each, and all the same shape: an edge from the writing thread to the reading thread, with the object crossing it. Choose one deliberately, because it worked on my machine is not an edge.
Records, immutability, and the limit of the freeze
Records (Java 16+) are built from final components, so a correctly constructed record arrives with the freeze already in place: JLS 17.5 applies its guarantee to record classes. The constructor a record generates does not let this escape, although a hand-written compact constructor can still do so, and then the usual rule applies to it. Reading a record through an unsafely published reference still shows the constructor values.
The limit is worth saying slowly: the freeze protects final fields, not the objects they reference. A record, or any class, holding a final reference to a mutable list still shares that list, and the guarantee is about what a reader sees at publication, never about what happens afterwards.
- Immutable by construction: final fields, no escape in the constructor, and no mutation afterwards. That recipe is what makes an object safe to share without further ceremony.
- String is the canonical example: final fields and an API with no mutators, so a published String needs no synchronization for its own state.
- A final reference to a mutable object: safe to read, still shared to mutate. Final freezes the field, not the object graph behind it.
final stops reassignment; the freeze is what makes publication safe. Neither one reaches through a reference, so immutability stays a property of the whole class, not of one keyword.
Where the freeze does not reach
Two corners move the guarantee off the ordinary path. Reflection can write a final field after setAccessible(true), and once a field has been modified outside construction the specification makes no promise about what other threads observe for it.
Deserialization constructs objects without running their constructors, so there is no construction freeze to appeal to; the java-serialization lesson owns that construction model, and a deserialized graph needs explicit coordination when it is shared. Both corners say the same thing: the guarantee is a fact about construction, not a property of the keyword in every context.
No constructor, no freeze. The rule you use day to day does not change: construct without escaping, then publish through a named edge. The corners only explain why the specification phrases the guarantee about constructors rather than about objects in general.
This escaped, and the guarantee left with it
A constructor that registers itself with a listener, starts a thread, or calls an overridable method has handed this to another thread before the freeze. The callback can run while the constructor is still on the stack and read final fields as their defaults. The fix is not a keyword: move the registration out of the constructor, after the freeze, and the same object becomes safely publishable. The failure is silent, intermittent, and invisible in review unless you are looking for publication points.
Interview angles
Does an unsafely published object with final fields need synchronization to be read correctly?+
Not for its final fields. JLS 17.5 guarantees that a thread which reads a reference to a completely initialized object sees the values the constructor assigned to its final fields, provided the object did not escape during construction. Non-final fields get no such guarantee.
The mechanism is the freeze at the end of the constructor: constructor writes to final fields stay before it, and reads of those fields cannot be reordered before the dereference of the reference. The rule is written against construction, so the object must actually finish being constructed before anyone can see it.
Common misconception: That final is only about reassignment. The keyword also carries a visibility guarantee that no other modifier provides.
Follow-up: What breaks the guarantee, and how would you spot it in review? An escape such as listener registration inside the constructor, possibly hidden behind an overridable method.
What does it mean for this to escape during construction?+
Any path that lets another thread observe the object before the constructor returns: registering this with a listener, starting a thread from the constructor, calling an overridable method that publishes it, or storing this in a static field. The observer can then read fields before the freeze.
Overridable methods are the sneaky case, because the base class constructor calls a method whose override is written by someone else and may hand this anywhere. The safe pattern is to keep constructors free of calls that can reach foreign code, or to restrict such calls to private and final methods.
Follow-up: How would you refactor a constructor that must register a callback? Move the registration to a factory method or an init step called after construction.
Does the freeze make a record immutable?+
No. Record components become final fields, so the freeze protects their values once construction completes, and JLS 17.5 applies its guarantee to record classes. A component that references a mutable object still shares that object.
Common misconception: That final components mean a deep immutable object. The freeze is one level deep, exactly like final itself.
How do you publish an object safely?+
Through a named happens-before edge: a static initialiser, a volatile reference, synchronized on both sides, a concurrent collection, or a Thread.start handoff. The freeze then covers the final fields, and the edge covers everything else.
Pick the edge deliberately and keep it in one place. The smell to watch for is publication through an ordinary field read by another thread with no coordination at all, which is where the half-built object comes from.
Does final make every field of the object visible?+
No. The freeze is about final fields written during construction. If every piece of state is a final field and the constructor does not escape, the whole object is safe once published; mix in plain mutable fields and those keep the ordinary unsafe publication risk.
This is why immutable classes are the standard answer for sharing across threads: when all state is final and set in the constructor, the freeze covers everything the object will ever need to show.
What about reflection and deserialization?+
Both take objects around their constructors. setAccessible(true) can write a final field, and the specification makes no promise about what other threads then observe; deserialization builds objects without running a constructor, so there is no freeze to appeal to. Share such objects with explicit synchronization.
Follow-up: Which lesson picks up the serialization side? The java-serialization lesson covers readObject and its construction model.