How do final instance fields interact with the Java Memory Model to provide safe publication, and what design rule does this imply for immutable objects?
answer
- final fields → JMM initialization safety / freeze at constructor end
- Other threads see fully initialized finals without synchronization
- Only if 'this' does not escape during construction
- Non-final fields need volatile/locks to publish safely
- Foundation of the immutable-object pattern
basics
~20 sThe Java Memory Model gives a special guarantee: if you set a final field during construction and don't let the object escape before the constructor finishes, any other thread that later sees the object is guaranteed to see the correctly initialized final field, even without locks or volatile.
solid answer
~50 sJava's Memory Model gives final fields a special initialization-safety guarantee. When a constructor that writes a final field exits, the field is 'frozen': any thread that obtains a reference to the fully constructed object, even through an ordinary unsynchronized read, is guaranteed to see the correctly initialized final fields, and anything reached through them at least as up to date. This is why a properly constructed immutable object (all fields final, no mutable state, and crucially no 'this' reference escaping during construction) can be shared across threads with no synchronization. The catch is the escape rule: if the constructor leaks 'this' (e.g. registers a listener, starts a thread, or stores itself in a shared field), another thread might see the object before the freeze, voiding the guarantee. A non-final field of the object gets no such guarantee and needs volatile, locking, or another safe-publication mechanism.
go deeper
May know that immutable objects are 'thread-safe' without the memory-model detail.
Knows final relates to immutability and that immutable objects are easier to share between threads.
Explains the final-field initialization-safety guarantee and that it requires no this-escape during construction.
Designs concurrency strategies around final-field safe publication, reasons about transitive reachability, this-escape hazards, defensive copying, and when volatile/locks are still required.
## Background: why visibility is even a problem In a multithreaded program, one thread writing a field does **not** automatically mean another thread will *see* that write. The **Java Memory Model (JMM)** is the part of the spec that defines when writes by one thread become visible to reads by another. Without proper synchronization, a thread can observe a **partially constructed** object, for example a reference that is non-null but whose fields still hold default values (0/null), because the compiler or CPU may reorder the field writes relative to the publication of the reference. **Safe publication** means making an object visible to other threads in a way that guarantees they see it fully and correctly initialized. ## The final-field guarantee The JMM grants `final` fields a special rule, sometimes called **initialization safety** or the **final-field freeze**. When a constructor that writes a final field exits, normally or abruptly, that field is **frozen** (JLS 17.5.1). If one constructor invokes another and the invoked one sets the field, the freeze comes at the end of the invoked constructor. A thread that can only see a reference to the object after its constructor has finished, even through an ordinary, unsynchronized field, is guaranteed to see the correctly initialized final fields. It also sees any object or array those fields refer to in a state at least as up to date as the fields themselves. This is unusual: normally you need `volatile`, `synchronized`, or another happens-before edge to publish data safely. Final fields bake that safety into construction itself. ## The critical precondition: do not let `this` escape The JLS usage model is: set the final fields in the constructor, and do not write a reference to the object where another thread can see it before the constructor finishes. If the constructor lets the `this` reference *escape* early, another thread may grab the object **before** the freeze and see uninitialized finals. Ways `this` escapes during construction: - Storing `this` in a static or shared field. - Passing `this` to a method that publishes it (e.g. registering an event listener, adding to a shared collection). - Starting a thread that captures `this` from inside the constructor. - Calling an overridable method that does any of the above. Since JDK 21, `javac -Xlint:this-escape` warns about calls to overridable methods in a constructor that might expose a partially constructed object to subclasses. Assigning a final before `super(...)` (JDK 25 and later) does not move the freeze: it still happens when the constructor exits, so a superclass constructor that leaks `this` to another thread is still an escape. So the design rule is: **finish constructing before publishing; never leak `this` from a constructor.** ## Why non-final fields are different A non-final field of the object receives **no** initialization-safety guarantee. The JLS example shows it: a racing reader that sees a non-null reference is guaranteed to read the final field's value, but the non-final `y` "could see 0". To publish such state safely you must use `volatile`, locks, or other happens-before machinery. The exception is state reached *through* a final field, which the guarantee does cover as far as it was written before the freeze. ## The immutable-object design pattern This directly motivates the canonical **immutable object** recipe for lock-free sharing: 1. Make **all fields `final`**. 2. Make the object's state **not mutable** after construction (no setters; defensively copy mutable inputs/outputs). 3. Ensure **`this` does not escape** during construction. 4. The class is then immutable and can be **freely shared across threads without synchronization**. ```java public final class Point { private final int x; private final int y; public Point(int x, int y) { this.x = x; this.y = y; } // no this escape public int x() { return x; } public int y() { return y; } } ``` A `Point` constructed on one thread and handed to others via any means is guaranteed to be seen fully initialized. ## Subtleties worth knowing - The guarantee covers what the final fields reference as of the freeze, so a `final` field pointing at an array gives visibility of the array's *contents written during construction*, but later mutations of that array are not covered (which is why you also need true immutability). - `String`, the boxed primitives, and other JDK immutables keep their state in final fields (`String.value`, `Integer.value`), which is what lets them be shared without locks. - The guarantee is about what a reader sees **through** a reference, not when the reference itself becomes visible. A racing reader can still read `null`, which is why the JLS example tests `f != null` first. - **Reflection can break the model.** The JLS says final fields "can be changed via reflection", and that the only pattern with reasonable semantics updates them before the object is visible to other threads or the fields are read; a freeze follows each such change. On JDK 25 LTS a deep-reflective write to a final instance field of an ordinary class is silent, on JDK 26 and 27 it warns by default, and JEP 500 plans to deny it by default in a future release. ## Summary Final fields give a special construction guarantee: a fully constructed object's final fields, and what they reference as of the freeze, are safely visible to any thread that later obtains the reference, with no synchronization, provided `this` never escaped during construction.
- What breaks the final-field safe-publication guarantee?Letting the this reference escape before the constructor completes — e.g. registering a listener, starting a thread that captures this, or storing this in a shared field. Then another thread can see the object before the final-field freeze and observe uninitialized values.
- Do non-final fields get any visibility guarantee from construction?Not on their own. A non-final field of the object needs explicit safe publication: volatile, synchronization, or another happens-before edge. The exception is state reached through a final field, such as an element of a final array, which the freeze covers as far as it was written before the freeze.
- Claiming all fields (not just final) get visibility guarantees for free
- Letting 'this' escape in the constructor and still expecting the guarantee
- Thinking final makes a referenced mutable object thread-safe for later mutations
- Confusing this guarantee with ordering of non-final writes
- final field semantics in the Java Memory Model
- Java 1.5
Assumes JDK 27; the memory-model rules are the same on JDK 25 LTS. Only deep reflection differs: a write to a final instance field of an ordinary class warns by default since JDK 26 and is silent on JDK 25.
context
- Chapter 17. Threads and Locksdocs.oracle.com
- JEP 500: Prepare to Make Final Mean Finalopenjdk.org