Singleton class in java – Includes Easy Implementation Steps

Learn singleton class in java with example. Singleton class is just a concept and we can make any class singleton which we want by following some rules. Singleton class restricts the instantiation of a class to single instance and ensures that only one instance of the class exists in the application. Users e.g. other classes and methods cannot create the object of singleton class. only class itself creates an object and provide a public static method to users to get the object. It is also known as singleton design pattern.

Once you understand singleton, you can read real time examples of singleton design pattern in java, means where and why you should use singleton class in java programs.

How to create singleton class in java

Here are some easy steps to make a class singleton with singleton class example program

Step -1: First we need to make a constructor of the class private so that object of the class cannot be created outside of the class.

Step -2: Create a private static object of the class itself as member field. It should be static because we will be using it in a static method that can access only static field.

Step -3: Create a static method e.g. getInstance() and return the object field created in the class itself. This method will be static so object can be called from outside of the class using class name, as user class or method cannot create an object. The object can be accessed by getInstance() method.

Example of Singleton class in Java

In this example of singleton class, the Car class has been made as Singleton and demonstrated the object calls in main () program.

class Car {

	/*
	 * -------------------------------------------- Create an object of the
	 * class it is static so that, can be called in static getInstance() method.
	 */

	private static Car obj = new Car();

	/*
	 * Make constructor private to stop object creation from outside of the
	 * class.
	 */
	private Car() {
		System.out.println("Constructor....");
	}

	/*----------------------------------------------
	 * Create a static method to return object to
	 * user places i.e. other classes and methods
	 * 
	 * User can call getInstance method using class
	 * name only.
	 */

	public static Car getInstance() {

		return obj;
	}

	public void run() {
		System.out.println("Car running....");
	}
}

/*
 * Test singleton Car class
 */

public class SingletonTest {

	public static void main(String[] args) {

		Car c1 = Car.getInstance();
		c1.run();

		Car c2 = Car.getInstance();
		c2.run();

		Car c3 = Car.getInstance();
		c3.run();

                //Lets Check if all 3 objects identity are same
		//by printing their hashcode
		System.out.println("Car c1 object hashcode: "+c1);
		System.out.println("Car c2 object hashcode: "+c2);
		System.out.println("Car c3 object hashcode: "+c3);

	}

}

Output:
Constructor….
Car running….
Car running….
Car running….
Car c1 object hashcode: Car@15db9742
Car c2 object hashcode: Car@15db9742
Car c3 object hashcode: Car@15db9742

Notice that constructor has been called only a single time while we have called object 3 times in main function. Meaning, only a single object has been created of the class and the same singleton class object is getting called again and again using getInstance() method of Car singleton class in Java program.

Secondly, to verify, if all the objects c1,c2 and c3 are same, we have print the identity of objects i.e. hashcode. All objects hashcode are same. Hence, it proves that we have created only one object in the memory.

Note that, we cannot print the address of objects. So, used hashcode.

NOTE: Above singleton class is not thread safe. You must read why singleton class should be double checked in java. Actually, it must be used in multi threaded environment.

Related Posts