Use the Singleton2
form when possible; synchronization is not required during access.
(The JVM may load the class at any time, but it should not initialize the Class object until first use )c
Java Language Specification, 12.4
static initializers should not execute until first use.c
in the constructor when program-shut-down cleanup activities (such as shutting down database connections in an orderly way) are required.
Do not use a finalizer, which may never be called. A private constructor prevents someone from saying new Singleton(), thereby forcing access through instance().
You have no requirement that only one instance of the Singleton exists, only that the number of instances are constrained and that access to the instances are global. cFor example, a
method may return one of a pool of database connections that the Singleton manages.
In UML, the role associated with the Singleton is usually also the class name.