Win a copy of Hands On Software Engineering with Python this week in the Jython/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
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

How readResolve() works internally?  RSS feed

 
Ranch Hand
Posts: 168
  • 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
 
Bartender
Posts: 9549
188
  • 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: 62172
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aren't Oracle going to deprecate serialisation?
 
Master Rancher
Posts: 364
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...).  

 
Sheriff
Posts: 23963
50
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.
 
Are we home yet? Wait, did we forget the tiny ad?
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!