• 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
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Serialization - readResolve method

 
Ranch Hand
Posts: 401
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If we have to prevent singleton objects from serialization, then we have to override readResolve method and thrown an Exception accordingly. I have certain queries:

1. What is the purpose of readResolve method?

2. What is the actual flow?

3. Is it used in any of the serialization/deserialization prcess?

4. Where is its default implementation defined?
 
Marshal
Posts: 64710
226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best way to protect singletons from deserialisation is to declare them as enum constants. Please find a copy of Thinking in Java by Bruce Eckel (the 2nd edition can be found free of charge: try the mindprod.com website) or Sierra Bates and Robson's cert exam revision guide. Both contain a good description of readResolve().
 
Saloon Keeper
Posts: 10310
217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You seem to be asking a lot of questions about singletons lately. Why are you using singletons? Why is it so important to you that you limit the instantiation of a class?

Anyway, the purpose of the readResolve() method is to replace the deserialized object with one that you actually want to use in your application. The reasons for this can be many.

A good example is when the data on disk looks very differently from the data in memory. You would then have one class that represents the data in memory, and one class that represents the data on disk. When you serialize an object, the serialization mechanism will call its writeReplace() method, where you can replace the object with an instance of the class that represents the data on disk. That object then gets serialized through the usual means. Later when you deserialize the object, the readResolve() method is called, where you can replace the object with an instance of the class as it appears in memory.

There is no default implementation for these classes. They don't normally exist. It's just that the serialization mechanism checks for their existence using reflection, and if they DO exist, only then are they called.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!