To make a singleton class serializable provide a readResolve method. Otherwise, each deserialization of a serialized instance will result in the creation of a new instance.
public class CacheMgr {
public static final CacheMgr INSTANCE = new CacheMgr();
private CacheMgr() {
// ...
}
// ... // Remainder omitted
// readResolve method to preserve singleton property
private Object readResolve() throws ObjectStreamException {
/*
* Return the one true CacheMgr and let the garbage collector
* take care of the CacheMgr impersonator.
*/
return INSTANCE;
}
public static void main(String[] args) {
System.out.println(CacheMgr.INSTANCE);
}
}
I have not thought of this aspect so far. So, obviously, I have no experience of it either. However, I am interesting to know if you know an idea on the subject. Any first hand experience?
2 comments:
You can also check out Implementing a Serializable Singleton.
Hi rajneesh,
Please guide me step by step installation on WinXp..
Apache config : please provide config in detail....
Post a Comment