最好的方法是使用枚举单例模式:
public enum MySingleton { INSTANCE;}这样可以保证对象的单例性,并以始终获得相同实例的方式为您提供可序列化性。
更一般地,您可以提供如下
readResolve()方法:
protected Object readResolve() { return myInstance;}
最好的方法是使用枚举单例模式:
public enum MySingleton { INSTANCE;}这样可以保证对象的单例性,并以始终获得相同实例的方式为您提供可序列化性。
更一般地,您可以提供如下
readResolve()方法:
protected Object readResolve() { return myInstance;}