Usually, when you create a Singleton, you will see that there is only one instance. you can do it like this:
public static class Singleton{ private final static Singleton instance; private Singleton(){} public static Singleton getInstance(){ if(instance == null) instance = new Singleton(); return instance; } }
Putting the constructor in private ensures that no one except this class can instantiate a Singleton.
You only need to call Singleton with getInstance, and Singleton do all the work on its side.
source share