• Post Reply Bookmark Topic Watch Topic
  • New Topic

Serialization of Singleton objects  RSS feed

 
karthik chellappan
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was going through 'Serialization of Singleton objects' concept.I got few threads where every one was saying it's possible using readResolve() method.

For e.g. Elvis is name of class for which we are going to create singleton object and we instantiate object with static modifier like shown below:



In first place,Serialization is for instance variables and not for class variables.static variables are not serialized/deserialized and hence a new object shouldn't be created right?

But then in the link i got, few guys are saying that a new object will be created by deserialization process and it is this new object which readResolve method ensures that nothing ever sees it.

Please clarify what exactly is happening behind the scenes when we are serializing/deserializing static variables?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Serialization doesn't have any effect on the static variable. It can write the Object pointed to by that static variable to long term storage, though. You can then read that back in to a new Object and store it in a different variable. Now you have two Objects of what is supposed to be a Singleton.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!