Image may be NSFW.
Clik here to view.
Clik here to view.

It’s a pretty well known pattern, but I want to discuss what a Singleton class is first. In a nutshell, a Singleton class is a class that will only have one instance of the class. In certain cases, we want to make sure that we cannot instantiate multiple copies of the object, so we limit it to just one copy. Instead of having a public constructor for our class, we use a private constructor. Then we use a public method (usually named getInstance()) to make sure there is only one copy.
Here is how it looks:
1
2
3
4
5
6
7
8
9
10
11
public class Singleton {
private static final Singleton instance;
private Singleton(){}
public static Singleton getInstance() {
if (instance == null)
instance = new Singleton();
return instance;
}
}
As you can see, the constructor is private, so we are unable instantiate it in the normal fashion. What you have to do is call it