• 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 ...
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
  • 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?
Posts: 4179
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
    Bookmark Topic Watch Topic
  • New Topic