How to Prevent Multiple Instances in a Singleton Class Java

Learn how to prevent multiple Instances in a Singleton Class in Java using the Singleton design pattern. A Singleton class is a design pattern that ensures only one object of a class is created throughout the application. To achieve this, the constructor is made private, and a static method is used to provide access to the single instance.

Correct Point-Wise Explanation

  • Initialize a class named SingletonTest that holds the reference to the object using the static variable h.
  • Use the count variable to track how many objects have been created.
  • Declare a SingletonTest constructor that executes whenever an object is created and increments count by 1.
  • Inside the getInstance() method, a new object is created only when count is less than 2.
  • During the first call to getInstance(), Object 1 is created and count becomes 1.
  • During the second call to getInstance(), Object 2 is created and count becomes 2.
  • During the third call to getInstance(), no new object is created because count is not less than 2, and the method returns Object 2.
  • The hashcode() method is used to display the object’s hash code.
  • Different hash codes indicate different objects, while the same hash code indicates that the references point to the same object.
  • Therefore, s1 and s2 are different objects, whereas s2 and s3 refer to the same object.
				
					public class SingletonTest {

    private static SingletonTest h = null;
    static int count = 0;

    private SingletonTest() {
        count++;
    }

    public static SingletonTest getInstance() {
        if (count < 2) {
            h = new SingletonTest();
        }
        return h;
    }

    public static void main(String[] args) {

        SingletonTest s1 = SingletonTest.getInstance();
        System.out.println(s1.hashCode());

        SingletonTest s2 = SingletonTest.getInstance();
        System.out.println(s2.hashCode());

        SingletonTest s3 = SingletonTest.getInstance();
        System.out.println(s3.hashCode());
    }
}
				
			

Output :-
366712642
1829164700
1829164700

What Is a Singleton Class in Java?

A Singleton Class is a class that allows only one instance (object) to be created throughout the application’s lifetime. It provides a global access point to that single object using a static method called getInstance().

				
					public class SingletonHello {

    private static SingletonHello instance;

    private SingletonHello() {
        System.out.println("Object Created");
    }

    public static SingletonHello getInstance() {
        if (instance == null) {
            instance = new SingletonHello();
        }
        return instance;
    }

    public static void main(String[] args) {

        SingletonHello s1 = SingletonHello.getInstance();
        SingletonHello s2 = SingletonHello.getInstance();
        SingletonHello s3 = SingletonHello.getInstance();

        System.out.println(s1.hashCode());
        System.out.println(s2.hashCode());
        System.out.println(s3.hashCode());
    }
}
				
			

Output :-
Object Created
366712642
366712642
366712642