Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

importance of readResolve method in java

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.




 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 29
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 128
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Prints true
 
Anil kumar Sakala
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Adam
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ....
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Vineet Yekurde
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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. :-)
 
Greenhorn
Posts: 24
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 24
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 24
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Winston for that reply
 
reply
    Bookmark Topic Watch Topic
  • New Topic