Amazon Interview Question
Software Engineer / DevelopersThanks for the details Ji and Garry...Just to summarize..so that it can help others.
Singlton => One instance of a class.
If you make the class implements serializable then there could be more instance of class that gets created during deserialization if called twice or used reset() methods on the stream. Similarly while serializing it can cause problem if serialization chain in broken or reset() is called during serialization.
And as static fields are not part of serialization, and it plays very important role in singleton there could be a problem of loosing the value.
Cloneable => If we call cloneable on the object then it would generate the another object hence returning another instance of a class.
if you serialize a singleton object and subsequently deserialize that object more than once, you will have multiple singleton instances.It beats the purpose and hence.
- Ji June 02, 2008Regarding cloneable, obvious - it allows the singleton to be cloned -again - not desirable.