Quick revision
The short answer
The constructor calls src.register(this) before it assigns handlers, so this escapes while the object is still being built. If the event source delivers an event on another thread at that moment, onEvent runs and finds handlers still null. Even making handlers final would not help, because the final field guarantee only holds when this does not escape during construction. The fix is a factory method that constructs the listener fully and only then registers it; starting a thread from a constructor causes the same problem.
What to remember
In this answer, we saw that registering this inside the constructor lets the event source call onEvent before handlers is assigned, which causes the startup NullPointerException, and that final does not help because the final field guarantee requires that this does not escape during construction. The fix is to construct the listener fully, ideally with final immutable fields, and register it from a factory method afterwards. Starting a thread in a constructor causes the same problem, and the same separation of construction from start-up solves it.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The constructor calls src.register(this) before it assigns handlers, so this escapes while the object is still being built. If the event source delivers an event on another thread at that moment, onEvent runs and finds handlers still null. Even making handlers final would not help, because the final field guarantee only holds when this does not escape during construction. The fix is a factory method that constructs the listener fully and only then registers it; starting a thread from a constructor causes the same problem.
Why this question is asked
Startup is when a system is most concurrent and least predictable: event sources start delivering, thread pools warm up, and components register with each other. A bug that throws NullPointerException only during the first seconds after startup, and only sometimes, usually points to an object being used before it is ready. Interviewers use this example because it looks harmless, the field is assigned in the constructor after all, and the explanation needs both ordinary reasoning and the memory model. In this answer, we will reproduce the failure, trace the timeline, see why final does not save us, apply the factory fix, and look at the related trap of starting threads in constructors.
1Reproducing the Failure
Here is the class with its event source. The source keeps a list of listeners and delivers events from its own dispatch thread.
When events are already flowing at startup, the dispatch thread can pick up the new listener as soon as register(this) adds it to the list, and call onEvent immediately. The constructor is still running, possibly still inside loadHandlers(), so handlers is null. The helpful NullPointerException message in modern JDKs even names the field.
2Tracing the Timeline
The key idea is that this escapes, meaning a reference to the object becomes visible to other code before the constructor has finished. From that moment, the object can be used in a state its author never intended to expose.
6 messages in order. Play them, or step through them one at a time.
The listener is used between registration and the end of its constructor.
Notice that this failure does not even need any memory-model subtlety. The handlers field is simply not assigned yet when onEvent runs; the order of the constructor's own statements causes it. There is a second, quieter problem on top: even after the constructor assigns handlers, there is no happens-before edge from that assignment to the dispatch thread, unless the source's registration happens to create one, so the dispatch thread may keep seeing null for a while.
3Why Making handlers final Does Not Help
A natural reaction is to declare the field final. Final fields do have a special guarantee in the Java Memory Model: a thread that obtains a reference to an object after its constructor finishes sees the final fields as the constructor set them, even without synchronization. But that guarantee has a precondition: the object must be properly constructed, which the specification defines as this not escaping during construction.
Here this escapes before the constructor ends, so the precondition fails. Even if we moved the assignment before register(this), a reference that escaped during construction is not covered by the final field guarantee, so another thread could still in theory see the default value. In practice, with the assignment first, the source's registration lock usually publishes it, but correctness would then depend on the source's internals rather than on our class. The robust answer is not to let this escape at all.
4Fix: Construct Fully, Then Register
The standard fix is a static factory method. The constructor only builds the object; the factory registers it after construction has completed.
Two things changed. The listener is registered only after its constructor returns, so the dispatch thread can never see it half-built. And handlers is now final and immutable, so the final field guarantee applies: any thread that receives the listener sees the list. The registration itself, if the source stores listeners in a thread-safe collection such as CopyOnWriteArrayList, also creates a happens-before edge. Making the constructor private forces every caller through the factory.
5The Same Trap: Starting a Thread in a Constructor
Starting a thread inside a constructor is the other common way to let this escape. The new thread usually runs a method of the object, so it can see fields that the constructor has not assigned yet.
Creating the thread object in the constructor is fine; starting it is not. The usual fix is a separate start() method, or a factory that constructs and then starts. In Spring applications, the framework solves this for us: lifecycle callbacks such as SmartLifecycle.start() or an ApplicationReadyEvent listener run after beans are fully constructed and wired, which is the right place to register listeners and start background work.
6Follow-up questions
Would making onEvent check for null be an acceptable fix?
It hides the symptom, but events that arrive before initialisation are silently dropped, and the visibility problem remains because there is still no guaranteed edge from the assignment to the dispatch thread. Fixing the construction order is simpler and correct.
Are inner classes and lambdas in constructors also a risk?
Yes, when they are handed to other code during construction. An anonymous listener or a lambda that refers to the outer object captures this, so registering it from the constructor lets this escape in the same way. Creating it is harmless; exposing it before the constructor ends is the problem.
Is it safe to call an overridable method from a constructor?
It is a related risk. The subclass override runs before the subclass constructor has initialised its own fields, so it may see nulls even on a single thread. Constructors should only call private or final methods, for the same reason that they should not register this.
7Conclusion
In this answer, we saw that registering this inside the constructor lets the event source call onEvent before handlers is assigned, which causes the startup NullPointerException, and that final does not help because the final field guarantee requires that this does not escape during construction. The fix is to construct the listener fully, ideally with final immutable fields, and register it from a factory method afterwards. Starting a thread in a constructor causes the same problem, and the same separation of construction from start-up solves it.
Open with
The problem is that this escapes during construction. The constructor calls src.register(this) first, which puts the half-built listener into the source's list, and only then assigns handlers. If the source is already dispatching events on another thread, it can call onEvent in between, and handlers is still null, so we get a NullPointerException, but only at startup and only when an event arrives in that window.
Then, if they want more
On top of the ordering problem, there is also no guaranteed happens-before edge from the constructor's write of handlers to the dispatch thread, so that thread might keep seeing null for a while. Making handlers final does not fix it, because the final field guarantee only applies to properly constructed objects, meaning this must not escape during the constructor.
The fix is to separate construction from registration: make the constructor private, build the handlers list there as an immutable final field, and add a static factory that creates the listener and only then registers it. Then the dispatch thread can only ever see a complete object, and the final field guarantee holds.
The same issue happens if a constructor starts a thread that uses the object, or registers a lambda or inner class that captures this.
For threads I create them in the constructor if I like but start them in a separate start method, and in Spring I would use lifecycle callbacks such as SmartLifecycle or an application-ready event, which run after the bean is fully built.
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 "class Listener { Listener(EventSource src) { src.register(this); this.handlers =...": https://scrivio-interview-library.netlify.app/java/java-memory-model/6a505e5741/