• Post Reply Bookmark Topic Watch Topic
  • New Topic

Concept of writeReplace() and readResolve() method in Java  RSS feed

 
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I had quetion about writeRepalce() and readResolve() method in Java Serialization. My limited knowledge about the methods is as per below:

writeReplace()
==============

private Object writeReplace() throws ObjectStreamException { }

This method allows the developer to provide a replacement object that will be serialized instead of the original one.

readResolve()
=============

private Object readResolve() throws ObjectStreamException () { }

It is called after Object is deserialized. It must return an Object that then becomes the return value of the readObject() method. Remember to add readResolve method to all typesafe enumerations in your legacy code and all the classes that follow the "singleton design pattern".


I just know this much about these methods. I don't know about their implementation. I just did rote learning of these 2 methods.

What if I want to explain what these 2 methods do to someone ?? How do I explain them ?? Currently I am not in a position to explain these 2 methods to someone. Also I have searched the net for a simple practical example explaining the working of these 2 methods but I couldn't find any. Please guide me through a simple practical example explaining the working of these 2 methods and provide me a link which gives a pratical example.

Thanks
Sid.
 
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would use these if the actual object you want to work with in your code is different from what you need to serialize the object.

The common example is instance-controlled classes. Other classes are not allowed to call their constructor because it shouldn't be possible to have more instances than the ones you already provide. Java's cloning and serialization mechanisms provide 'magic' ways to create new instances anyway. The writeReplace() and readResolve() methods allow your application to (de)serialize representations of these instances, and then convert between the representation and the controlled instance.

Another example is if you have written a class which enforces its invariants very strictly, which make it difficult for the serialization mechanism to create new instances. Let's say you have written an immutable class with final member variables, and you need to perform validation in the constructor before you can guarantee that the class is in a valid state. Because the serialization mechanism requires you to have a parameterless constructor that can't do validation, your type can't be deserialized. The solution here is to use writeReplace() to replace your object with a mutable representation (usually just a javabean) which gets serialized, and upon deserialization the readResolve() method is called to convert the bean back to your strict type.
 
Siddharth Bhargava
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:You would use these if the actual object you want to work with in your code is different from what you need to serialize the object.

The common example is instance-controlled classes. Other classes are not allowed to call their constructor because it shouldn't be possible to have more instances than the ones you already provide. Java's cloning and serialization mechanisms provide 'magic' ways to create new instances anyway. The writeReplace() and readResolve() methods allow your application to (de)serialize representations of these instances, and then convert between the representation and the controlled instance.

Another example is if you have written a class which enforces its invariants very strictly, which make it difficult for the serialization mechanism to create new instances. Let's say you have written an immutable class with final member variables, and you need to perform validation in the constructor before you can guarantee that the class is in a valid state. Because the serialization mechanism requires you to have a parameterless constructor that can't do validation, your type can't be deserialized. The solution here is to use writeReplace() to replace your object with a mutable representation (usually just a javabean) which gets serialized, and upon deserialization the readResolve() method is called to convert the bean back to your strict type.



Please could you refer me to a practical example to run and understand???
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!