This week's book giveaway is in the Python forum.
We're giving away four copies of Python Continuous Integration and Delivery and have Moritz Lenz on-line!
See this thread for details.
Win a copy of Python Continuous Integration and Delivery this week in the Python forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

How readResolve() works internally?  RSS feed

 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We serialize a Singleton object and save into a file Saved.txt. Now from a different jvm if we deserialize then deserialization use reflection and create a object with same instance values saved in the file.But it is a different object, so we get different hashCode.
But if we use readresolve() method in our singleton class then we will get same hashCode.
So the question is how we are getting a same hashCode?
The object we are getting now,should be a new object only not that one which we created at the time of serialization.

Our Singleton class is provided below—



Thanks
Abhra
 
Saloon Keeper
Posts: 9846
199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When readObject() is called on an ObjectInputStream, it will deserialize the Serializable object from the input stream, and then the readResolve() method will be called on the deserialized object. So even though a new instance of Singleton is deserialized, the invocation of readResolve() on that new object will return the shared instance that was previously made and stored in the static instance field.

This code nicely demonstrates how useless it is to make an instance controlled type serializable.

Try to avoid the Serializable class if you can help it. Definitely avoid the singleton anti-pattern.
 
Marshal
Posts: 63409
205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aren't Oracle going to deprecate serialisation?
 
Rancher
Posts: 371
13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Aren't Oracle going to deprecate serialisation?



I heard they were....

Although articles still abound on the Oracle website about serialisation (one example is this old article here), I have heard "rumblings" that indeed Oracle appears to want to deprecate serialisation, mainly due to security gaps. Some in the tech media are speculating the beginning of it's demise (such as DZone).

To their credit, Oracle did produce documentation warning about serialisation/deserialisation (Secure Coding...).  

 
Marshal
Posts: 24191
54
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Definitely avoid the singleton anti-pattern.



And if you persist in having a singleton, then don't use antiquated code which works in a way which is difficult to comprehend.  Here's a more modern way:



This code doesn't require tricky synchronization to create the single instance of the enum and (regarding your question) it can be serialized and deserialized in the way you want.
 
He was giving me directions and I was powerless to resist. I cannot resist this tiny ad:
Become a Java guru with IntelliJ IDEA
https://www.jetbrains.com/idea/
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!