Quick revision
The short answer
The lock is null after deserialization because Java does not run field initialisers or constructors of a serializable class when it rebuilds an object, and a transient field is not read from the stream either. Since the field is final, we cannot simply assign it in readObject. The two fixes are to make the field non-final and create the lock in readObject, or to add readResolve so deserialization returns a new instance built through the constructor; often the best design is to serialize a plain data form instead of an object that carries a lock.
What to remember
In this answer, we saw that the transient final lock is null after deserialization because Java skips the constructors and field initialisers of serializable classes, and because the field is final, readObject cannot simply assign it. We fixed it by making the field non-final and creating the lock in readObject, or by keeping final and returning a constructor-built instance from readResolve, with a warning about back-references. The cleanest design avoids serializing objects that carry locks and stores a plain data form instead.
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 lock is null after deserialization because Java does not run field initialisers or constructors of a serializable class when it rebuilds an object, and a transient field is not read from the stream either. Since the field is final, we cannot simply assign it in readObject. The two fixes are to make the field non-final and create the lock in readObject, or to add readResolve so deserialization returns a new instance built through the constructor; often the best design is to serialize a plain data form instead of an object that carries a lock.
Why this question is asked
This bug shows up in caches, sessions and anywhere objects are copied between JVMs. The class looks perfect: the lock is created right where it is declared, it is final so it can never be null, and the code has worked for months. Then someone puts the object into a distributed cache, and every call on the copy fails. Interviewers like it because the answer needs two pieces of knowledge at once: how deserialization builds objects and what final means for assignment. In this answer, we will reproduce the failure, explain why the lock is null, walk through two fixes and a third design option, and compare them.
1Reproducing the NullPointerException
Here is a simple thread-safe counter. The lock is marked transient because a lock's state, who holds it right now, means nothing in another JVM.
Notice that count survived the round trip while lock did not. The field was never written, because it is transient, and nothing created a new one on the way back in.
2Why the Final Field Is Null
The initialiser = new ReentrantLock() is compiled into Counter's constructor. During deserialization, ObjectInputStream allocates a Counter without calling that constructor; it only runs the no-argument constructor of the first non-serializable superclass, here Object. It then sets the non-transient fields, count, from the stream. A transient field is left with the default value the JVM gave the fresh object, which for a reference is null.
Normally final means a field is assigned exactly once in a constructor, and the compiler guarantees it is never seen unassigned. Deserialization goes around that guarantee: the object is created without the constructor, so the final field is simply never assigned. So final did not protect us; it only removes the obvious repair, because the compiler refuses lock = new ReentrantLock() anywhere except a constructor or initialiser.
An interesting detail: ReentrantLock itself implements Serializable, and a deserialized ReentrantLock comes back unlocked. So simply removing transient would also make this code work. But serializing synchronisation objects is a poor habit, because it ties our data format to concurrency details, and it would fail for other resources that are not serializable. The fixes below are cleaner.
3Fix 1: Make the Field Non-Final and Set It in readObject
The smallest change is to drop final and create the lock in a private readObject method, after reading the normal fields.
We lose the compiler's guarantee that the field is assigned once, but it is still private and only these two places assign it. readObject runs before the object is handed to anyone else, so no other thread can see the null value, and the normal rules for publishing objects safely still apply after that.
4Fix 2: Return a New Instance From readResolve
readResolve is a method that ObjectInputStream calls after it has built the object. Whatever readResolve returns replaces the deserialized object in the result. We can use it to throw away the half-built copy and return an instance created through a real constructor, where the final field initialiser does run.
6 messages in order. Play them, or step through them one at a time.
This keeps final and pushes every instance through a constructor, which is also where we would put validation. But there is a catch: if the deserialized object is part of a cycle, another object in the same stream may have been given a reference to the original, lock-less instance before readResolve replaced it. For a simple value like this counter that cannot happen, but for object graphs with back-references, Fix 1 or the next option is safer.
5A Better Design: Serialize Plain Data, Not Objects With Locks
Both fixes keep sending a behaviour-rich object across the wire. Often what we really want to keep is just the number. A cleaner design is to give the class a plain data form for storage, such as a record, and rebuild the live object from it. The serialization proxy pattern does this inside Java serialization: writeReplace writes a small record instead of the Counter, and the record's readResolve builds a new Counter through its constructor.
| Option | Keeps final | Main cost |
|---|---|---|
| Remove transient | yes | serializes a lock; fails for non-serializable resources |
Non-final + readObject | no | field can be reassigned inside the class |
readResolve | yes | unsafe with back-references in object graphs |
| Proxy or plain data form | yes | a little more code; the cleanest format |
Now the stream only ever contains CounterData, the lock never travels, and every Counter is made by a constructor. Outside Java serialization the same idea is even simpler: store the count as JSON or in a database row and create a new Counter when we load it.
6Follow-up questions
Why not just mark the lock non-transient?
ReentrantLock happens to be serializable and comes back unlocked, so it would work here. But it couples the stored data to a concurrency detail, wastes bytes, and the same pattern fails as soon as the field is a connection or another resource that cannot be serialized. Treating locks as local, rebuildable state is the better habit.
Could we set the final field with reflection in readObject?
It is possible with Field.setAccessible(true) on a non-static final field, but it is fragile, hidden from readers, and may be restricted by the module system and future JDK rules on final field mutation. When we feel the need for it, readResolve or the serialization proxy is the honest way to keep final.
Would a record solve this?
A record cannot hold mutable state like count++, so it does not replace Counter, but it is the ideal data form for the proxy. Records are deserialized through their canonical constructor, so their own invariants are always checked.
7Conclusion
In this answer, we saw that the transient final lock is null after deserialization because Java skips the constructors and field initialisers of serializable classes, and because the field is final, readObject cannot simply assign it. We fixed it by making the field non-final and creating the lock in readObject, or by keeping final and returning a constructor-built instance from readResolve, with a warning about back-references. The cleanest design avoids serializing objects that carry locks and stores a plain data form instead.
Open with
The NullPointerException happens because deserialization does not run the constructor of a serializable class, and a field initialiser like new ReentrantLock() is really part of the constructor. ObjectInputStream allocates the object, runs only the no-argument constructor of the first non-serializable superclass, and fills in the non-transient fields from the stream.
Then, if they want more
The lock is transient, so it is not in the stream, and nothing creates it, so it stays null. Because it is final, I cannot just assign it in readObject; the compiler rejects that. My first fix is to remove final and assign a new ReentrantLock in a private readObject method right after defaultReadObject.
The field stays private and is assigned before anyone else sees the object, so it is safe. My second fix keeps final: add readResolve that returns a new instance built through a constructor, passing the deserialized count, so the initialiser runs and the half-built copy is thrown away.
I would mention that readResolve can be unsafe when other objects in the same stream point back at this one. And honestly, I would rather not serialize an object that carries a lock at all.
I would serialize a plain data form, such as a record holding the count, through a serialization proxy or as JSON, and rebuild the live object from it.
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
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
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 "A class holds a transient final ReentrantLock lock = new ReentrantLock(). After...": https://scrivio-interview-library.netlify.app/java/serialization/f17a13a5e8/