wood burning stoves 2.0*
The moose likes Java in General and the fly likes Singleton class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Singleton class" Watch "Singleton class" New topic
Author

Singleton class

amarkirt saroay
Ranch Hand

Joined: Mar 16, 2008
Posts: 167
Can a singleton class be serializable?


SCJP-75%
SCWCD-82%
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
Yes, but it will no longer be a true singleton.

Singleton ---> same object ---> same object ---> same object ---> serialise ---> deserialise ---> different object
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
Probably not easy enough a question for beginners. Moving.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Campbell Ritchie wrote:Yes, but it will no longer be a true singleton.

Singleton ---> same object ---> same object ---> same object ---> serialise ---> deserialise ---> different object

That's what the readResolve and writeReplace are for, especially the former:

But Joshua Bloch has given a very good alternative in his "Effective Java" book: use an enum with only one element:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
Thank you Rob. I had quite forgotten about readResolve.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Singleton class
 
Similar Threads
singleton class
Speaks anything against this Singleton implementation?
singleton class
ClassLoader is Singleton ?
what is a singleton class?