Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What does safe publication mean, and list the ways to publish an object safely in Java.

Safe publication means making an object available to other threads in a way that guarantees they see its fully constructed state, not just a reference to it. The standard ways are to initialise it in a static initializer, store the reference in a volatile field or an AtomicReference, store it in a final field of an object that is itself properly constructed, or store it in a field guarded by a lock or put it into a thread-safe collection.

Why this question is asked

Every object shared between threads has to get from the thread that creates it to the threads that use it. Most of the time we do this without thinking, through a Spring bean, a queue or a static field. When we do it wrongly, another thread can get a reference to an object whose fields still look like defaults, and the bug appears only under load or on a different CPU. Interviewers ask about safe publication because it is the practical side of the memory model: it tells us how to share objects correctly. In this answer, we will see what goes wrong with unsafe publication, define safe publication precisely, go through each safe way with code, and see what publication does not cover.

1What Goes Wrong Without It

Creating an object involves several writes: the JVM allocates memory and sets default values, the constructor writes the fields, and then the reference is stored somewhere. Without synchronization, the Java Memory Model does not require another thread to see these writes in that order. The compiler or CPU may make the reference visible before the field writes.

Java
class Holder {
int value;
Holder(int v) { value = v; }
}
class Registry {
static Holder shared; // plain static field
}
// Thread 1
Registry.shared = new Holder(42);
// Thread 2
Holder h = Registry.shared;
if (h != null) System.out.println(h.value); // may print 0

Thread 2 can see a non-null reference and still read value as 0, the default, because there is no happens-before edge between the constructor's write in thread 1 and the read in thread 2. The object was published unsafely. On x86 this is rare; on ARM it can happen in practice.

2What Safe Publication Means

Publishing an object means making a reference to it reachable by another thread. Safe publication means doing it in a way that creates a happens-before edge from the end of the construction to every thread that obtains the reference. Then those threads see the reference and also everything the constructor wrote, and anything written before publication.

Safe publication is about the state at the moment of publication. If the object is immutable, that is all we need: other threads can use it freely forever. If it is mutable and changes later, those later changes need their own synchronization.

3The Ways to Publish Safely

Java offers several mechanisms, each creating the edge in a different way.

Java
// 1. Static initializer: class initialisation is run under a lock by the JVM
static final Holder DEFAULT = new Holder(42);
// 2. Volatile field or AtomicReference
private volatile Holder current;
current = new Holder(42); // readers that see it see value == 42
AtomicReference<Holder> ref = new AtomicReference<>();
ref.set(new Holder(42));
// 3. Final field of a properly constructed object
final class Wrapper {
private final Holder holder;
Wrapper(Holder h) { this.holder = h; } // holder and what it reached are visible
}
// 4. A field guarded by a lock
private Holder guarded; // guarded by this
synchronized void set(Holder h) { guarded = h; }
synchronized Holder get() { return guarded; }
// 5. A thread-safe collection
ConcurrentMap<String, Holder> map = new ConcurrentHashMap<>();
map.put("k", new Holder(42)); // a thread that gets it sees value == 42
BlockingQueue<Holder> queue = new LinkedBlockingQueue<>();
queue.put(new Holder(42));
Safe publication mechanisms
MechanismEdge comes fromTypical use
Static initializerClass initialisation lockConstants, eager singletons
volatile or AtomicReferenceVolatile write before readReplaceable snapshots, lazy init
final field of a built objectFinal field freeze at constructor endImmutable objects and their parts
Lock-guarded fieldUnlock before later lockMutable state with invariants
Concurrent collection or queueDocumented put before getHanding objects between threads

A static initializer is safe because the JVM initialises a class under a lock and every thread that uses the class first synchronizes on its initialisation. A volatile write happens-before every read that sees it, and the constructor's writes come before it in program order. The final field rule says that a thread that sees a properly constructed object sees its final fields, and everything reachable through them, at least as up to date as at the end of the constructor. A lock works because an unlock happens-before the next lock of the same monitor. And the concurrent collections document that putting an object in happens-before another thread retrieving it.

4Publication That Frameworks Do for Us

Much of our code publishes objects through frameworks that already do this correctly. Submitting a task to an ExecutorService happens-before the task runs, so objects captured by the task are safely published to it. Completing a CompletableFuture happens-before the dependent stages and get() see the result. In Spring, singleton beans are created and wired during context startup, and the container's internal synchronization publishes them before request threads use them, which is why injected fields of singletons work even though they are not volatile. The rule still applies to state we change after startup: a mutable field in a singleton that request threads update is not covered.

5What Safe Publication Does Not Cover

Safe publication guarantees the state at the moment of publication. It does not make a mutable object thread-safe afterwards. If we publish an ArrayList through a volatile field and then keep adding to it from one thread while others read it, the readers have no guarantee about those later additions, and concurrent modification can break the list.

There is also a construction-time trap: an object is only properly constructed if this does not escape during the constructor, for example by registering this as a listener or starting a thread that uses it. If it escapes, other threads can see it before construction finishes, and even the final field guarantee does not hold. The simplest safe design combines both lessons: build the object completely, preferably as an immutable object with final fields, then publish it through one of the mechanisms above.

6Follow-up questions

Is an immutable object safe even if published through a data race?

If all its fields are final and this did not escape during construction, yes: the final field guarantee means any thread that gets the reference sees the constructor's values, even through a racy plain field. That is what makes String safe to share. An object that is merely never modified, with non-final fields, does not get this guarantee and needs safe publication.

Does publishing through a ConcurrentHashMap make the object thread-safe?

It makes its state at the time of put visible to threads that get it. If the object is mutable and is changed after it was put, those changes are not covered. For mutable values, either replace the value with a new immutable object through put or compute, or protect the object's own state with a lock.

Is Collections.synchronizedList a safe way to publish?

Yes, adding an object to a synchronized collection publishes it, because the collection's lock creates the edge between the add and a later get. The same caution applies: later changes to the object itself are not covered.

7Conclusion

In this answer, we saw that without safe publication another thread can see a reference to an object before it sees the object's fields. Safe publication creates a happens-before edge from construction to the reader, and Java gives us five ways to do it: a static initializer, a volatile field or AtomicReference, a final field of a properly constructed object, a lock-guarded field, and a thread-safe collection. It covers the state at publication time only, so mutable objects still need their own synchronization, and this must not escape during construction.

What to say in the interviewAbout 100 seconds aloud

Open with

Safe publication means handing an object to other threads so that they see its fully constructed state, not just the reference. Without it, there is no happens-before edge between the constructor's writes and the other thread's reads, so a thread can see a non-null reference and still see default field values.

Then, if they want more

The safe ways are these. A static initializer, because the JVM runs class initialisation under a lock that every thread synchronizes on before using the class. A volatile field or an AtomicReference, because the volatile write happens-before every read that sees it, and the constructor's writes come before that write.

A final field of an object that is itself properly constructed, because final fields and everything reachable through them are guaranteed visible after the constructor finishes. A field guarded by a lock, used under the same lock for writing and reading.

Or a thread-safe collection like ConcurrentHashMap or a BlockingQueue, which documents that putting an object happens-before another thread retrieving it. Frameworks use these for us too: submitting a task to an executor publishes what it captures, and Spring publishes singleton beans during context startup. Two caveats.

Safe publication covers the state at the moment of publication, so if the object is mutable and changes later, those changes still need synchronization; that is why I prefer immutable objects. And the object must be properly constructed, meaning this does not escape during the constructor, for example by registering a listener or starting a thread.

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 "What does safe publication mean, and list the ways to publish an object safely in Java.": https://scrivio-interview-library.netlify.app/java/java-memory-model/96cefae6ff/