Scrivio Interview library
InternalsIntermediate6 min read · updated 12 November 2026

What visibility guarantee does the Java Memory Model give for final fields, and how can code break it?

If an object's constructor finishes without letting this escape, any thread that later sees a reference to the object is guaranteed to see its final fields, and everything reachable through them as of the end of construction, correctly initialized, even if the reference was shared with no synchronization at all. Code breaks this by leaking this from the constructor, by relying on non-final fields, which need safe publication through volatile, locks or concurrent collections, or by changing final fields with reflection, which other threads may never see.

Why this question is asked

Most of the time we share objects between threads through something that synchronizes, such as a ConcurrentHashMap or a lock, and visibility takes care of itself. The final field guarantee matters in the cases where it does not: lazy caches written without locks, objects handed over through a plain field, and the internals of immutable classes such as String. Interviewers ask about it at this level to check that we know the exact promise and its conditions, because getting either wrong produces bugs that appear only on some hardware. In this answer, we will recap why visibility is a problem at all, state the guarantee precisely, show code that relies on it, and then go through the three ways code breaks it.

1A Quick Recap: Why Another Thread Can See a Half-Built Object

Creating an object involves several writes to memory: the fields set by the constructor, and then the write that stores the reference somewhere other threads can read it. Without synchronization, the Java Memory Model allows those writes to become visible to another thread in a different order. The compiler can reorder them, and so can the CPU, particularly on ARM processors, which reorder stores more freely than x86.

So a thread reading a shared field can see a non-null reference to an object and still read the default values, null or 0, from that object's fields. Publishing an object through a data race, an unsynchronized write read by another thread, is called unsafe publication.

2The Guarantee, Stated Precisely

The Java Memory Model, in JLS section 17.5, makes a special promise for final fields. At the end of a constructor, the final fields are frozen. Any thread that obtains a reference to the object after that point sees the final fields with the values they had at the freeze, without any synchronization. The promise extends through the final field: if a final field refers to an array or another object, a reader that reaches it through the final field sees its contents as they were at the end of construction.

There is one condition: the reference must not have been made available to other threads during construction. That is what correctly constructed means here.

Java
final class Point {
final int x;
int y; // not final
Point(int x, int y) { this.x = x; this.y = y; }
}
class Holder {
static Point shared; // plain field: publication through a data race
}
// Thread A
Holder.shared = new Point(3, 4);
// Thread B
Point p = Holder.shared;
if (p != null) {
System.out.println(p.x); // always 3: final field guarantee
System.out.println(p.y); // 4, or possibly 0: no guarantee for non-final fields
}
Step throughUnsafe publication: final and non-final fields
Thread AMemoryThread B
write x = 3 (final)
write y = 4 (plain)
constructor ends: final fields frozen
write shared = reference
read shared: non-null
x: guaranteed 3
y: 4 or 0, either is allowed

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

Only the freeze at the end of the constructor orders the final field write before the reference.

Notice that the same object, published the same way, gives a guarantee for one field and not for the other. This is how String stays safe: its value array and its coder are final, so a String shared through a race still shows its full contents.

3What Reaches Through a Final Field

The guarantee covers objects reachable from a final field, as of the end of construction. That makes a common immutable pattern safe without any locks.

Java
final class Palette {
private final int[] colors;
Palette() {
colors = new int[] {0xFF0000, 0x00FF00};
colors[1] = 0x0000FF; // still inside the constructor: covered
}
int second() { return colors[1]; }
}
// Any thread that sees a Palette through a race: second() returns 0x0000FF
// But a write to colors[1] made after construction is NOT covered; it needs synchronization

So the rule is about a moment in time. Whatever the constructor wrote through the final field is visible; whatever anyone writes later is ordinary shared mutable state and needs the usual tools.

4Way 1 to Break It: Letting this Escape

If the constructor stores this in a shared place, registers it as a listener, or starts a thread that uses it, another thread can obtain the reference before the freeze. For that thread, the guarantee does not apply, and it can see default values even in final fields.

Java
final class Sensor {
static Sensor latest; // shared
final String name;
Sensor(String name) {
latest = this; // escapes before the freeze
this.name = name;
}
}
// Another thread reading Sensor.latest.name can see null

The fix is to finish construction first and publish afterwards, for example from a static factory method.

5Way 2 and Way 3: Non-Final Fields and Reflection

Non-final fields have no special guarantee. To share an object with mutable or non-final fields, we must publish it safely: store it in a volatile field, write and read it under the same lock, put it in a concurrent collection such as ConcurrentHashMap, or initialize it in a static initializer, which the class initialization lock protects. Any of these creates a happens-before relationship, the memory model's term for "everything written before is visible after".

Reflection is the third way. Field.setAccessible(true) followed by Field.set() can still change a final instance field of an ordinary class today, but the memory model gives no guarantee about when other threads see the new value, and the JIT may have already treated the field as a constant and keep using the old one. Final fields of records and hidden classes, and static final fields, cannot be changed this way at all. The JDK is preparing to restrict deep reflection on final fields further, with JEP 500 adding warnings first; check your version for what is enforced.

How each kind of field is published
FieldVisible to other threads whenNeeds
final, this did not escapereference is seen, even through a racenothing more
final, this escapednot guaranteeddo not leak this
non-finalonly after a happens-before edgevolatile, lock, concurrent collection
final changed by reflectionnot guaranteed at alldo not do it

6Follow-up questions

Does the guarantee make the whole object thread-safe?

No. It only makes the initial values of final fields visible. If the object holds a mutable collection in a final field and someone changes it later, those changes are ordinary shared writes that need synchronization. Immutability needs final fields and immutable contents together.

Why does double-checked locking need volatile even if the object has final fields?

If every field of the object is final and the constructor is correct, a reader that sees the reference sees correct fields, so the classic bug of seeing a half-built object goes away. But volatile is still the standard advice: it makes the published reference itself visible in a predictable way, and it works for objects with non-final fields. Today we usually use the holder idiom or a library instead.

Is this a problem on x86?

Much less in practice, because x86 does not reorder stores with other stores, so the race is hard to observe there. But the JIT compiler can still reorder, and ARM servers such as AWS Graviton reorder far more freely. Code must follow the Java Memory Model, not the behaviour of one CPU.

7Conclusion

In this answer, we saw that without synchronization another thread can see a reference to an object before it sees the object's fields, and that the Java Memory Model makes an exception for final fields: once the constructor finishes without leaking this, every thread that sees the reference sees the final fields, and what is reachable through them, as of the end of construction. The guarantee breaks when this escapes during construction, does not exist for non-final fields, which need volatile, locks or concurrent collections, and does not cover final fields changed later by reflection.

What to say in the interviewAbout 90 seconds aloud

Open with

The guarantee is in JLS 17.5. When a constructor finishes, the object's final fields are frozen, and any thread that afterwards obtains a reference to the object sees those final fields with their constructed values, without any synchronization, even if the reference was handed over through a data race.

Then, if they want more

It also covers what is reachable through the final field as of the end of construction, so a final array filled in the constructor is seen with its contents. That is what makes immutable classes like String safe to share. The condition is that the object is correctly constructed, which means this did not escape during construction.

If the constructor registers this, stores it in a static field or starts a thread with it, another thread can get the reference before the freeze, and then even final fields can appear as null or zero.

Non-final fields have no such guarantee at all, so for those I need safe publication: a volatile field, a lock, a concurrent collection or a static initializer, anything that creates a happens-before edge.

And changing a final field with reflection breaks it too, since other threads, or JIT-compiled code that folded the value, may never see the change; static finals and record fields cannot be changed that way, and the JDK is moving to restrict this further, so I would check the version in use.

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 "What visibility guarantee does the Java Memory Model give for final fields, and how can...": https://scrivio-interview-library.netlify.app/java/classes-and-objects/221e1d24ee/