Scrivio Interview library
InternalsIntermediate7 min read · updated 12 November 2026

Why are immutable objects thread-safe, and what does the Java Memory Model guarantee about final fields? When can an object with final fields still be seen in an inconsistent state?

Immutable objects are thread-safe because their state never changes after construction, so there is nothing for threads to race on. The Java Memory Model adds a guarantee for final fields: once the constructor finishes, any thread that sees a reference to the object sees the final fields as the constructor set them, plus the objects reachable through them as of that moment, even without synchronisation. That guarantee fails if this escapes during construction, does not cover later changes to objects reachable through a final field, and does not apply to non-final fields, which need safe publication.

Why this question is asked

Threads in Java can see each other's writes late, out of order or not at all unless the program creates the right ordering. That is why a value written by one thread may look wrong in another, even on a single object. Immutable objects avoid most of this, but the reason they are safe is a specific rule of the memory model, with specific limits. Interviewers ask about it at this level to see whether we know the rule precisely enough to spot the cases where it breaks. In this answer, we will recap the visibility problem, explain the final field guarantee, break it by letting this escape, look at what it does and does not cover, and see what safe publication means for everything else.

1A Quick Recap: Why Threads Can See Stale Values

The Java Memory Model, or JMM, is the part of the language specification that says which writes one thread is guaranteed to see from another. Without a guarantee, the compiler, the JIT and the CPU are allowed to reorder and cache memory operations for speed. So if thread A creates an object and stores a reference in a shared field, thread B may see the reference before it sees the values the constructor wrote into the object's fields.

Normally we create the ordering we need with a happens-before relationship: unlocking a lock happens-before a later lock of the same lock, a write to a volatile field happens-before a later read of it, and so on. Code that shares objects without any such relationship has a data race.

Java
class Holder { int value; Holder(int v) { value = v; } } // value is NOT final
static Holder shared; // plain field, no volatile
// Thread A
shared = new Holder(42);
// Thread B
Holder h = shared;
if (h != null) System.out.println(h.value); // allowed to print 0: the write to value may not be visible yet

Printing 0 is rare on x86 hardware, but the JMM allows it, and it can happen on weaker processors such as ARM or after JIT reordering. Code that relies on luck here is broken.

2The Final Field Guarantee

The JMM has a special rule for final fields (JLS section 17.5). At the end of a constructor, there is a conceptual freeze of the object's final fields. Any thread that obtains a reference to the object after that, through any route, even a data race like the one above, is guaranteed to see the final fields with the values the constructor assigned. The only condition is that the object was properly constructed, which means this did not escape before the constructor finished.

Java
final class Point {
private final int x, y;
Point(int x, int y) { this.x = x; this.y = y; } // freeze of x and y at the end of the constructor
int x() { return x; }
}
static Point shared; // still a plain field
// Thread A
shared = new Point(1, 2);
// Thread B
Point p = shared;
if (p != null) System.out.println(p.x()); // guaranteed 1, never 0
Step throughFinal fields frozen at the end of the constructor
Thread APoint objectShared fieldThread B
x = 1, y = 2 (final)
freeze at end of constructor
shared = point
read shared
read x
1, guaranteed

6 messages in order. Play them, or step through them one at a time.

Any thread that sees the reference after the freeze sees the final values, even through a data race.

This is why immutable objects are safe to share. Their state is set once, in the constructor, in final fields, and never changes. No thread can see a partly built state, and since there are no later writes, there is nothing to race on afterwards. String depends on exactly this guarantee, which is why a String passed between threads through a plain field still has its characters.

3Breaking the Guarantee: Letting this Escape

The freeze happens at the end of the constructor, so the guarantee only protects threads that see the reference after that point. If the constructor gives this to another thread before finishing, that thread can see the final fields before they are set, and the JMM promises nothing.

Java
final class Listener {
private final String name;
Listener(EventBus bus, String name) {
bus.register(this); // this escapes: the bus thread may call onEvent() right now
this.name = name; // assigned after the escape
}
void onEvent(Event e) { System.out.println(name.length()); } // may throw NullPointerException
}
// Fix: finish construction, then publish
static Listener create(EventBus bus, String name) {
Listener l = new Listener(name); // constructor no longer registers itself
bus.register(l); // published after the freeze
return l;
}

Other ways this escapes are starting a thread in the constructor that uses this, passing this to a static registry, or creating an inner class or lambda instance that captures this and handing it to other code. The fix is always the same: complete construction first, then publish, typically from a static factory method.

4What the Guarantee Covers and What It Does Not

The guarantee extends past the field itself. If a final field refers to an object, such as an array or a list, other threads see that object and everything reachable from it at least as up to date as it was at the end of the constructor. So an immutable class that fills an ArrayList in its constructor, stores it in a final field and never changes it again is safe to share.

But the guarantee is a snapshot of the moment of the freeze. If anyone changes the reachable object later, through a getter that exposed it or through the caller's reference, those changes have no special visibility at all. Other threads may or may not see them, in any order. That is one more reason to copy mutable inputs and never expose internal objects.

And the guarantee applies only to final fields. A non-final field in the same object, even one assigned in the constructor, has no such promise. That is why an object that is effectively immutable, never changed after construction but without final fields, is only safe if it is published safely.

What another thread is guaranteed to see
SituationGuaranteed?
final field set in constructor, no escapeyes, even through a data race
object reachable through a final field, as of constructor endyes
later change to that reachable objectno
non-final field set in constructorno, unless safely published
final field when this escaped during constructionno

5Safe Publication for Everything Else

Safe publication means making an object visible to other threads in a way that creates a happens-before relationship between the constructor and the readers. Then all fields written before publication, final or not, are visible. The common ways are: initialising the reference in a static initialiser, storing it in a volatile field or an AtomicReference, storing it in a final field of another properly constructed object, storing it in a field guarded by a lock that readers also take, or putting it into a thread-safe collection such as ConcurrentHashMap or a BlockingQueue.

For immutable objects with final fields, safe publication is not needed for the object's own state, but it is still needed for the reference itself if readers must see the newest object rather than an older one. In practice we usually publish immutable snapshots through a volatile field or AtomicReference anyway, which gives both.

6Follow-up questions

What is safe publication?

It is any way of handing an object to other threads that guarantees they see it fully constructed. The JDK gives us several: a static initialiser, a volatile or AtomicReference field, a final field of a properly constructed object, a field protected by a lock that both sides use, and the thread-safe collections in java.util.concurrent. Each creates a happens-before edge from the writes in the constructor to the reads in other threads.

Is volatile needed on a field that holds an immutable object?

Not for the object's contents, which the final field rule protects. It is needed if readers must reliably see a new object after a writer replaces the old one. Without volatile, a reader might keep seeing the old reference indefinitely, for example inside a loop the JIT has optimised.

Does the final field guarantee hold for fields changed by reflection after construction?

No. Changing a final field with reflection after construction has no defined visibility for other threads, and the JIT may have already treated the value as constant. Since recent JDKs, changing final fields through reflection is being restricted further, so check your version; it should not be used in normal code.

7Conclusion

In this answer, we saw that immutable objects are thread-safe because their state never changes, so no thread can race on it, and that the JMM backs this with the final field rule: once the constructor finishes, every thread that sees the object sees its final fields, and what they referred to at that moment, as the constructor left them, even without synchronisation. The rule breaks if this escapes during construction, it does not cover later changes to objects reachable through final fields, and it does not apply to non-final fields, which need safe publication through volatile, locks, static initialisers or concurrent collections.

What to say in the interviewAbout 90 seconds aloud

Open with

Immutable objects are thread-safe because there are no writes after construction, so there is nothing to race on, and the memory model makes sure the construction itself is seen correctly.

Then, if they want more

Normally, if one thread publishes an object through a plain field, another thread could see the reference before the field values, so it might read zero or null.

For final fields, JLS 17.5 gives a special guarantee: at the end of the constructor the final fields are frozen, and any thread that later sees the reference, even through a data race, sees those final values.

It also covers objects reachable through the final field as they were at the end of the constructor, so an array filled in the constructor and never changed is visible correctly. There are three limits.

First, the object must be properly constructed: if this escapes during construction, for example by registering a listener or starting a thread in the constructor, another thread can see the final fields before they are set. The fix is to publish from a static factory after construction. Second, later changes to a reachable mutable object are not covered at all.

Third, non-final fields get no guarantee, so an effectively immutable object without final fields must be safely published, through a volatile or AtomicReference, a static initialiser, a lock, or a concurrent collection.

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.
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 "Why are immutable objects thread-safe, and what does the Java Memory Model guarantee...": https://scrivio-interview-library.netlify.app/java/immutability/fbcd7007d0/