Generally, Singleton
design pattern concept is based on having only single instance
of your class
. This could be reached through two main aspects:
1) Having a private constructor for your class to prevent any outer class to call it and re-create the instance. This could be reached as the following:
private Singleton()
{
System.out.println("Singleton(): Initializing Instance");
}
2) Having a static
method that allow you to retrieve the initialized instance
or initialize it if it is not initialized yet as @Awfully Awesome mentioned in his answer:
public static Singleton newInstance()
{
if (singleton == null)
{
singleton = new Singleton();
}
return singleton;
}