• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Serialization of Singleton objects

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
The problems of the world fade way as you eat a piece of pie. This tiny ad has never known problems:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic