Take the 2-minute tour ×

The book Effective Java and other sources provide a pretty good explanation on how and when to use the readObject() method when working with serializable Java classes. The readResolve() method, on the other hand, remains a bit of a mystery. Basically all documents I found either mention only one of the two or mention both only individually.

Questions that remain unanswered are:

  • What is the difference between the two methods?
  • When should which method be implemented?
  • How should readResolve() be used, especially in terms of returning what?

I hope you can shed some light on this matter.

share|improve this question

6 Answers 6

up vote 41 down vote accepted

readResolve is used for replacing the object read from the stream. The only use I've ever seen for this is enforcing singletons; when an object is read, replace it with the singleton instance. This ensures that nobody can create another instance by serializing and deserializing the singleton.

share|improve this answer
There is a number of way for malicious code (or even data) to get around that. –  Tom Hawtin - tackline Jul 22 '09 at 21:33
Yes, please explain. Flyweight patterns rely on this working, so how can it break? –  Steve Armstrong Mar 18 '10 at 15:46
Josh Bloch talks about the conditions under which this breaks in effective Java 2nd ed. Item 77. He mentions about this in this talk he gave in Google IO couple of years back (some times towards the end of the talk): youtube.com/watch?v=pi_I7oD_uGI –  calvinkrishy Sep 18 '10 at 3:26
Book page mentioned by @calvinkrishy: books.google.co.uk/… –  TWiStErRob Jun 2 at 21:42

readResolve is called after readObject has returned (conversely writeReplace is called before writeObject and probably on a different object). The object the method returns replaces this object returned to the user of ObjectInputStream.readObject and any further back references to the object in the stream. It is mostly used for serial proxies (see Effective Java, 2nd Ed, IIRC).

share|improve this answer


share|improve this answer

readResolve() will ensure the singleton contract while serialization.
Please refer

share|improve this answer

readResolve can be used to change the data that is serialized through readObject method. For e.g. xstream API uses this feature to initialize some attributes that were not in the XML to be deserialized.


share|improve this answer
XML and Xstream aren't relevant to a question about Java Serialization, and the question was answered correctly years ago. -1 –  EJP May 5 '13 at 0:45
The accepted answer states that readResolve is used to replace an object. This answer provides the useful additional information that it can be used to modify an object during deserialization. XStream was given as an example, not as the only possible library in which that happens. –  Enwired Feb 26 at 1:00

readResolve is for when you may need to return an existing object, e.g. because you're checking for duplicate inputs that should be merged, or (e.g. in eventually-consistent distributed systems) because it's an update that may arrive before you're aware of any older versions.

share|improve this answer

Your Answer


