Quick revision
The short answer
writeReplace lets an object nominate a different object to be written in its place, and readResolve lets a freshly deserialized object nominate a different object to be returned to the caller. A serializable singleton uses readResolve to return its one instance so deserialization does not create a second one, although an enum does this automatically and more safely. The serialization proxy pattern combines both: writeReplace writes a small private proxy, the proxy's readResolve rebuilds the real object through its public constructor, and the real class's readObject throws so nobody can bypass the proxy.
What to remember
In this answer, we saw that writeReplace chooses what object is written and readResolve chooses what object is returned after reading. A class-based singleton needs readResolve returning its instance, and transient fields, to stay single, while an enum gets that guarantee from the JVM. The serialization proxy pattern writes a simple private proxy through writeReplace, rebuilds the real object through its public constructor in the proxy's readResolve, and blocks direct streams with a throwing readObject.
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.
writeReplace lets an object nominate a different object to be written in its place, and readResolve lets a freshly deserialized object nominate a different object to be returned to the caller. A serializable singleton uses readResolve to return its one instance so deserialization does not create a second one, although an enum does this automatically and more safely. The serialization proxy pattern combines both: writeReplace writes a small private proxy, the proxy's readResolve rebuilds the real object through its public constructor, and the real class's readObject throws so nobody can bypass the proxy.
Why this question is asked
These two methods are how careful library authors tame Java serialization. They let a class decide what form it has on the wire and what object comes back, which is the difference between a class whose invariants can be broken by a crafted byte stream and one whose invariants always hold. Interviewers ask about them because they test a deeper understanding of serialization than "implement Serializable", and they lead naturally to the question of why enums are the best singletons. In this answer, we will recap where the two hooks sit in the process, break a singleton with serialization and fix it with readResolve, see why an enum is better, and then build the serialization proxy pattern step by step.
1A Quick Recap: Where the Two Hooks Run
When ObjectOutputStream is about to write an object, it first checks whether the class has a writeReplace() method returning Object. If so, it calls it and writes whatever comes back instead. When ObjectInputStream has finished reading an object, it checks whether that object's class has a readResolve() method. If so, it calls it and returns its result to the caller instead of the object it just built. Both methods can have any access modifier, though private is usual for final classes, and @Serial documents them.
6 messages in order. Play them, or step through them one at a time.
writeReplace chooses what is written; readResolve chooses what is returned.
2Breaking a Singleton With Serialization
A singleton is a class with exactly one instance. If it is serializable, deserialization creates a new object without calling any constructor, and the private constructor that guards the "exactly one" rule is simply bypassed.
Code that relies on identity, or on state held by the one instance, now behaves wrongly, and every deserialization adds another instance.
3Fixing It With readResolve
We add readResolve and return the existing instance. The freshly built object is discarded and becomes garbage; the caller receives the real singleton.
But there is a catch. The copy is still fully built before readResolve runs, and during that time other objects in the same stream can grab a reference to it. If the singleton has non-transient fields that hold object references, a crafted stream can steal a reference to the temporary copy, so all instance fields of such a singleton should be transient. The fragility of this is exactly why enums are preferred.
4Why an Enum Is the Better Singleton
Enums are serialized differently from all other classes. The stream contains only the enum's class and the constant's name. On reading, the JVM calls Enum.valueOf with that name and returns the existing constant. No new object is ever allocated, the enum's fields are never written or read, and any writeObject, readObject, readResolve or writeReplace methods an enum declares are ignored.
So the guarantee comes from the platform rather than from code we must get right. Reflection cannot create new enum instances either, so an enum singleton is protected against both serialization and reflective attacks.
5The Serialization Proxy Pattern
For ordinary value classes, the proxy pattern gives us the same kind of guarantee. Instead of serializing the real class's fields, we serialize a small private static nested class that holds just the logical state. writeReplace on the real class swaps in the proxy when writing. readResolve on the proxy turns it back into a real object by calling the public constructor, so every check in that constructor runs. And readObject on the real class throws, so a hand-crafted stream that contains the real class directly is rejected.
| Technique | Invariants enforced by | Weak points |
|---|---|---|
| Default serialization | nothing on read | any crafted stream is accepted |
readObject with checks | repeated checks in readObject | checks can drift from the constructor |
Singleton with readResolve | returning INSTANCE | temporary copy can leak; fields must be transient |
| Enum singleton | the JVM itself | cannot extend a class |
| Serialization proxy | the public constructor | a bit slower; awkward with cyclic graphs |
Notice what this buys us. The real class can keep final fields and no readObject validation logic; the constructor is the single place where the rules live. The serialized form is the proxy's two fields, so we can change Money's internals freely without breaking old streams. And there is no temporary half-built Money for an attacker to capture.
6Limits of the Proxy Pattern
The pattern has costs. It is somewhat slower, because every object is built twice, once as a proxy and once as the real thing. It does not work well for classes in object graphs with cycles back to themselves, because during reading, other objects that refer back to the instance receive the proxy rather than the real object, which can cause a ClassCastException. And it cannot be used on classes meant to be extended by clients. Records largely give us this pattern for free: they are always rebuilt through their canonical constructor.
7Follow-up questions
Why is an enum the preferred singleton for serialization?
Because enum serialization is handled by the JVM itself: only the constant's name is written, and reading looks up the existing constant with valueOf, so a second instance can never be created and no code of ours has to be correct. Reflection cannot instantiate enums either. A class-based singleton depends on a correctly written readResolve and transient fields, and still briefly creates a copy.
Can readResolve return a different type than the class it is declared in?
Yes, it returns Object, and in the proxy pattern it does exactly that: the proxy's readResolve returns a Money. The caller of readObject receives whatever readResolve returns, so it must be compatible with what callers expect.
Does writeReplace affect inheritance?
If writeReplace is not private, subclasses inherit it, which may make them write the parent's proxy and lose their own state. That is one reason the proxy pattern is used on final classes and the methods are usually private.
8Conclusion
In this answer, we saw that writeReplace chooses what object is written and readResolve chooses what object is returned after reading. A class-based singleton needs readResolve returning its instance, and transient fields, to stay single, while an enum gets that guarantee from the JVM. The serialization proxy pattern writes a simple private proxy through writeReplace, rebuilds the real object through its public constructor in the proxy's readResolve, and blocks direct streams with a throwing readObject.
Open with
writeReplace and readResolve let a class control what crosses the wire. When an object is serialized, ObjectOutputStream first calls writeReplace if the class has one and writes whatever it returns.
Then, if they want more
After an object is deserialized, ObjectInputStream calls readResolve if present and gives the caller that result instead. For a singleton that implements Serializable, deserialization would create a second instance, because constructors are skipped, so I add readResolve returning INSTANCE; the copy is thrown away.
I would also make its fields transient, because the copy exists briefly and a crafted stream could grab references from it. Better still, I use an enum: enums are serialized by name and resolved with valueOf, so no new instance is ever created and none of my code has to be right. The serialization proxy pattern uses both hooks.
The real class's writeReplace returns a private static nested proxy holding only the logical state, the proxy's readResolve calls the real class's public constructor, so all validation runs, and the real class's readObject throws InvalidObjectException so nobody can feed it a direct stream.
It costs a little speed and does not suit cyclic graphs or extensible classes, and records already behave much like this because they always go through their canonical constructor.
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 "What do readResolve and writeReplace do? Explain how they are used for singletons and...": https://scrivio-interview-library.netlify.app/java/serialization/81b9509cb8/