Sometimes it's appropriate to have exactly one instance of a class: window managers, print spoolers, and filesystems are prototypical examples. Typically, those types of objects—known as singletons—are accessed by disparate objects throughout a software system, and therefore require a global point of access. Of course, just when you're certain you will never need more than one instance, it's a good bet you'll change your mind.
The Singleton design pattern addresses all of the previous paragraph's concerns. With the Singleton design pattern you can:
Although the Singleton design pattern—as evidenced below by the figure below—is one of the simplest design patterns, it presents a number of pitfalls for the unwary Java developer. This article discusses the Singleton design pattern and addresses those pitfalls.
Note: You can download this article's source code from Resources.
In Design Patterns, the authors describe the Singleton pattern like this:
Ensure a class has only one instance, and provide a global point of access to it.
The figure below illustrates the Singleton design pattern class diagram.

Singleton class diagram
As you can see from the figure above, there's not a whole lot to the Singleton design pattern. Singletons maintain a static
reference to the sole singleton instance and return a reference to that instance from a static instance() method.
Example 1 shows a classic Singleton design pattern implementation:
public class ClassicSingleton {
private static ClassicSingleton instance = null;
protected ClassicSingleton() {
// Exists only to defeat instantiation.
}
public static ClassicSingleton getInstance() {
if(instance == null) {
instance = new ClassicSingleton();
}
return instance;
}
}
The singleton implemented in Example 1 is easy to understand. The ClassicSingleton class maintains a static reference to the lone singleton instance and returns that reference from the static getInstance() method.
There are several interesting points concerning the ClassicSingleton class. First, ClassicSingleton employs a technique known as lazy instantiation to create the singleton; as a result, the singleton instance is not created until the getInstance() method is called for the first time. This technique ensures that singleton instances are created only when needed.
Second, notice that ClassicSingleton implements a protected constructor so clients cannot instantiate ClassicSingleton instances; however, you may be surprised to discover that the following code is perfectly legal:
public class SingletonInstantiator {
public SingletonInstantiator() {
ClassicSingleton instance = ClassicSingleton.getInstance();
ClassicSingleton anotherInstance =
new ClassicSingleton();
...
}
}
How can the class in the preceding code fragment—which does not extend ClassicSingleton—create a ClassicSingleton instance if the ClassicSingleton constructor is protected? The answer is that protected constructors can be called by subclasses and by other classes in the same package. Because ClassicSingleton and SingletonInstantiator are in the same package (the default package), SingletonInstantiator() methods can create ClassicSingleton instances. This dilemma has two solutions: You can make the ClassicSingleton constructor private so that only ClassicSingleton() methods call it; however, that means ClassicSingleton cannot be subclassed. Sometimes, that is a desirable solution; if so, it's a good idea to declare your singleton class final, which makes that intention explicit and allows the compiler to apply performance optimizations. The other solution is to
put your singleton class in an explicit package, so classes in other packages (including the default package) cannot instantiate
singleton instances.
This doesn't work for meBy Anonymous on October 24, 2009, 7:24 pmWhen I implement your pattern and have a singleton in a different package to the SingletonRegistry the registry gets an access violation and cannot load the singleton...
Reply | Read entire comment
thanksBy Anonymous on October 1, 2009, 1:35 pmthanks
Reply | Read entire comment
This article was exactly what I was looking for.By Anonymous on September 27, 2009, 11:54 amThis article was exactly what I was looking for.
Reply | Read entire comment
Breaking the SingletonBy Anonymous on September 23, 2009, 9:17 amI found this article very useful, but in the following blog post i have read the singleton pattern could be broken. any comments on this?? http://yohanliyanage.blogspot.com/2009/09/breaking-singleton.html
Reply | Read entire comment
goodBy Anonymous on September 17, 2009, 5:46 amtoo good
Reply | Read entire comment
View all comments