Wednesday 8 July 2015

Serialization for Singleton Object

We can serialize the Singleton Object without any issue but it will return the new (different) INSTANCE of that class.

To AVOID this scenario (multiple instance), implement the readResolve() method and return the Singleton instance.
   

private  Object readResolve() throws ObjectStreamException{
       return singletonInstance;
}

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...