public class MySingleton { private static final MySingleton INSTANCE = new MySingleton(); private MySingleton() {} public static final MySingleton getInstance() { return INSTANCE; } /** * Normal deserialization returns a new instance of an object. This ensures that only one instance is in existence. * Deserialization can either create a new instance and leave the deserialized object to be garbage collected or * reuse the deserialized instance. */ private Object readResolve() throws ObjectStreamException { return INSTANCE; } }
You need to create an account or log in to post comments to this site.
- lazy initialization
- exception/error handling (or logging, at least) for the constructor call