my dog learned polymorphism*
The moose likes Java in General and the fly likes importance of readResolve method in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "importance of readResolve method in java " Watch "importance of readResolve method in java " New topic
Author

importance of readResolve method in java

Anil kumar Sakala
Greenhorn

Joined: Oct 02, 2009
Posts: 29
I dont understand the importance of read resolve method with respect to singleton design pattern.

public class Elvis {
private static final Elvis INSTANCE = new Elvis();
private Elvis() {
...
}
public static Elvis getInstance() {
return INSTANCE;
}
... // Remainder omitted
}

In the effective java book it is said that if the above class implements serializable then read resolve method has to be included to enforce singleton design pattern. The read resolve method looks like this

public object ReadResolve()
{
return Instance;

}

from the above code each time i deserialize a Elvis class readResolve() is called and read resolve is going to return the same object that has been just deserialized. If i deserialize 10 times each time readResolve() method will return new object that is just deserialized . Where is the singleton ? Each time duirng deeserialization new instance of class is created and read resolve method replaces the deserialized object with the new instance that is created.




Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

readResolve wont create new Instance it return existing Instance . if you are not implement readResolve then deserialize create new Instance that is why readResolve needed
Adam Michalik
Ranch Hand

Joined: Feb 18, 2008
Posts: 128
It's the deserialization process that creates the new instance. But readResolve() returns the private static final instance of Elvis, thus overriding the instance returned by the deserialization process.
Anil kumar Sakala
Greenhorn

Joined: Oct 02, 2009
Posts: 29
Adam ,

Each time we deserilize read resolve returns public static final variable value which is getting populated with new instance() , for every deserialization.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Anil kumar Sakala wrote:
Each time we deserilize read resolve returns public static final variable value which is getting populated with new instance() , for every deserialization.


No.How you tested?
Adam Michalik
Ranch Hand

Joined: Feb 18, 2008
Posts: 128


Prints true
Anil kumar Sakala
Greenhorn

Joined: Oct 02, 2009
Posts: 29
Thank you Adam
Vineet Yekurde
Greenhorn

Joined: Jul 15, 2012
Posts: 6
Seetharaman Venkatasamy wrote:readResolve wont create new Instance it return existing Instance . if you are not implement readResolve then deserialize create new Instance that is why readResolve needed



Well if that's the case, then what the use of serializing the object if object state is somewhat different than the current state.

Please consider following program .




Output is :
Executing constructor
An instance is returned
Values are :i=23 d=45.4
Object modified
Values are :i=5 d=5.5
An instance is returned
Serialized successfully
Object modified
Values are :i=1 d=1.1
Executing readResolve
An instance is returned
Deserialized successfully
Values are :i=1 d=1.1

As per above , what i serialized is i=5,d=5.5. Buf after deserialization what i get is : i=1,d=1.1
is this fine ? No ....
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7492
    
  18

Vineet Yekurde wrote:As per above , what i serialized is i=5,d=5.5. Buf after deserialization what i get is : i=1,d=1.1
is this fine ? No ....

Actually: Yes.

First off, the idea of a mutable Singleton seems bizarre; but even given that, presumably the whole point of having a singleton is that there's only one of them, and it should always reflect the latest state. You appear to want your de-serializer to act as a "resetter", which strikes me as:
(a) Dangerous.
(b) If you absolutely must have it work that way, you've written your readResolve() method wrong: What it needs to do is get the instance and update it before returning it.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Vineet Yekurde
Greenhorn

Joined: Jul 15, 2012
Posts: 6
Winston Gutkowski wrote:
Vineet Yekurde wrote:As per above , what i serialized is i=5,d=5.5. Buf after deserialization what i get is : i=1,d=1.1
is this fine ? No ....

Actually: Yes.

First off, the idea of a mutable Singleton seems bizarre; but even given that, presumably the whole point of having a singleton is that there's only one of them, and it should always reflect the latest state. You appear to want your de-serializer to act as a "resetter", which strikes me as:
(a) Dangerous.
(b) If you absolutely must have it work that way, you've written your readResolve() method wrong: What it needs to do is get the instance and update it before returning it.

Winston


Well , that's fine.
Thanks for the reply. I got the scene now. :-)
Shiv Vishwakarma
Greenhorn

Joined: May 29, 2007
Posts: 21

Hi,

I have created a singleton class and then serialized it and after deserialization it is returning a different instance which ideally should not happen, I read at many places that overriding the readResolve() we can handle this problem. but I am still have this one doubt, where exactly this readResolve method is present that we should override.
I tried to add this method from my own side in the singleton class implementing serializable interview, but this does not seems to be working.

Any help would be greatly appreciated.
Thanks in advance.

Regards,
Shiv


Vishwakarma, Shiv
SCJP 1.4, SCWCD 5
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7492
    
  18

Shiv Vishwakarma wrote:I read at many places that overriding the readResolve() we can handle this problem. but I am still have this one doubt, where exactly this readResolve method is present that we should override.

To be honest, I don't quite know; and I'm not even sure if it "overrides" anything. The way I view it is that serialization is an inbuilt function in the language, which is triggered by the presence of the Serializable marker interface. If the class you're planning on putting readResolve() in is final, Bloch suggests that you actually make it private; and, as we all know, private methods can't "override" anything.

The fact is that you're probably best off simply defining the object as an enum, because that way you don't have to worry about readResolve() at all.

Winston
Shiv Vishwakarma
Greenhorn

Joined: May 29, 2007
Posts: 21

Thanks Winston for your reply,
I have been this question many times by many people and everytime I answered them that overriding the readResolve method we can avoid having multiple instances of singleton class. But today actually when I am trying it, I am not able to do it. And if we are not overriding this method then are we providing this method from our side, and how the JVM knows whether the readResolve method is present and it has to return the already created instance of singleton class.
Please provide any working example of the same, as I tried and it didn't work

Shiv
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7492
    
  18

Shiv Vishwakarma wrote:But today actually when I am trying it, I am not able to do it. And if we are not overriding this method then are we providing this method from our side, and how the JVM knows whether the readResolve method is present and it has to return the already created instance of singleton class.

Like I say, I'm not sure of the exact mechanics; you may find it in the JLS. Just be assured that it does know, even if your readResolve() method is private.
Please provide any working example of the same, as I tried and it didn't work

As I said above, the simplest one I can think of, based on your example, is:which is guaranteed to work because
(a) All enums are Serializable.
(b) They handle the instance control automatically.

HIH

Winston
Shiv Vishwakarma
Greenhorn

Joined: May 29, 2007
Posts: 21

Thanks Winston for that reply
 
Don't get me started about those stupid light bulbs.
 
subject: importance of readResolve method in java
 
Similar Threads
Use Serialization judicially
ClassLoaders
Singleton Pattern
singleton class
When is it appropriate to use a singleton pattern?