Singleton Class in Java

Singleton Class in Java

Singleton classes, as the name suggests, are those classes in Java that can only be instantiated once. This means that singleton classes can only have one object at any given time. This, however, does not mean that you cannot instantiate again. If you do so, the new variables that will be formed will just point out to the same object again. Therefore, any modification done to any variable of any instance will only reflect in the single instance that has been created of the Singleton class. This change is visible if that variable is accessed through any variable of that class type defined.

To design a singleton class, the following needs to implemented:

  1. The constructor of the class has to be defined as private.
  2. A static method has to be written that will have the return type object of this singleton class. To write this static method, the concept of lazy initialization can be used.

Normal vs Singleton Classes

When dealing in terms of instantiation, the difference between normal and singleton classes is that, a constructor is used to initialize the fields and variables of a normal class whereas, for singleton classes, the getInstance() method is generally used as a Java convention. Sometimes, the name of the singleton class is also used as the method name at method declaration.

Sample Code

The following code illustrates the creation of a Singleton Java class with a getInstance() method.

class Singleton
{
private static Singleton single_instance = null.

public String s;

private Singleton()
{
s = “Hello this is part of a new Singleton class”;
}

public static Singleton getInstance()
{
if (single_instance == null)
single_instance = new Singleton ();

return single_instance;
}
}

class Main
{
public static void main (String args [])
{
Singleton x = Singleton.getInstance ();
Singleton y = Singleton.getInstance ();
Singleton z = Singleton.getInstance ();

x.s = (x.s).toUpperCase();

System.out.println (“String from x is” + x.s);
System.out.println (“String from y is” + y.s);
System.out.println (“String from x is” + z.s);

z.s = (z.s).toLowerCase();

System.out.println (“String from x is” + x.s);
System.out.println (“String from y is” + y.s);
System.out.println (“String from z is” + z.s);

}
}

 

OUTPUT

String from x is HELLO THIS IS PART OF A NEW SINGLETON CLASS
String from y is HELLO THIS IS PART OF A NEW SINGLETON CLASS
String from z is HELLO THIS IS PART OF A NEW SINGLETON CLASS

String from x is hello this part of a new singleton class
String from y is hello this part of a new singleton class
String from z is hello this part of a new singleton class

Explanation of the Code

In a singleton class, when we first call the getInstance() method, this creates an object in the of the singleton class with the name ‘single_instance’ amd return it to the variable. The method is assigned some object from null since the method is static. The single_instance method returns the original variable whenever the method is called next. This is because the method is no longer null and instantiation is not done all over again.

Three objects are instantiated in the main class namely x, y, and z by calling static method getInstance(). Originally, only the object x is created. Objects y and z actually refer to the same variable object x. Therefore, if any change to the variables to the object x is made, this will reflect throughout the entire programm that is, both in y and z. Similarly, if either the variables of object y or z is changed, this is reflected in the other objects as well.

class Singleton
{
private static Singleton single_istance = null;

public String s;

private Singleton ()
{
s = “Hello this is part of the Singleton class”;
}

public static Singleton Singleton ()
{
if (single_instance == null)
{
single_instance = new Singleton ();
}
return single_instance;
}
}

class Main
{
public static void main (String args [])
{
Singleton x = Singleton.Singleton ();
Singleton y = Singleton.Singleton ();
Singleton z = Singleton.Singleton ();

x.s = (x.s).toUpperCase();

System.out.println(“String from x is ” + x.s);
System.out.println(“String from y is ” + y.s);
System.out.println(“String from z is ” + z.s);

z.s = (z.s).toLowerCase();

System.out.println(“String from x is ” + x.s);
System.out.println(“String from y is ” + y.s);
System.out.println(“String from z is ” + z.s);

}

}

OUTPUT

String from x is HELLO THIS IS PART OF THE SINGLETON CLASS
String from y is HELLO THIS IS PART OF THE SINGLETON CLASS
String from z is HELLO THIS IS PART OF THE SINGLETON CLASS

String from x is hello this part of the singleton class
String from y is hello this part of the singleton class
String from z is hello this part of the singleton class

Explanation of Code

When the Singleton () method of the singleton class is called, an object of this class is created under the name of single_instance and variable is returned. Initially, single_instance was null as it was static however after object creation, this has been changed to some value. Whenever the method is called next time, as the variable single_instance is not null, it is returned to the variable instead of instantiating the Singleton class again.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.