what will happen if I serialize and de-serialize a singleton instance in same JVM?
We will get two instances of Singleton object. To avoid that we should use a readResolve() method which returns the singleton instance.
Implementing a Serializable Singleton:
public class MySingleton implements Serializable
{
static MySingleton singleton = new MySingleton();
private MySingleton() {
}
// This method is called immediately after an object of this class is deserialized.
// This method returns the singleton instance.
protected Object readResolve()
{
return singleton;
}
}
You might also like ..
what is JIT - Just In Time Compiler?
Difference between page and pageContext
JDBC 3.0 features
Can we serialize static variables?
Difference between page and pageContext
JDBC 3.0 features
Can we serialize static variables?
No comments:
Post a Comment