By: RAHUL SHARMA
What is Singleton class..? If we can create only one object of java class then that java class is said to be Singleton class. OR Singleton class is a class that can have only one object at a time. for e.g. Runtime BusinessDelegate
Why use Singleton class… If there are several similar requirement and we have create only one object and reuse the same object for every similar requirement. The purpose of singleton is to control object creation by keeping private constructor. The main advantage of singleton class is improvement of performance and memory utilization.
e.g. Runtime r1 = Runtime.getRuntime(); Runtime r2 = Runtime.getRuntime(); . . . . . Runtime rn = Runtime.getRuntime(); Object r1 r2 rn Note : We can create singleton class objects by using factory methods. As we can see that for n number of references only one object has been created.
Can we create our own singleton classes..? Yes, we can create our own singleton classes. To create the user-defined singleton classes we have to use private constructor, private static variable and public factory method. There are two approaches to create singleton class
Approach 1 : Step 1: Create a Singleton class class Test1 { private static Test1 t = new Test1(); private Test1() { System.out.println("Approach 1 executed successfully."); } public static Test1 getTest() { return t; } } Step 2: Access the object of Singleton class class Test2 { public static void main(String[] args) { Test1 t = Test1.getTest(); } } Output : Approach 1 executed successfully.
Approach 2 : Step 1: Create a Singleton class class Test1 { private static Test1 t = null; private Test1() { System.out.println("Appro ach 2 executed successfully."); } public static Test1 getTest() { if(t == null) { t = new Test1(); } return t; } } Step 2: Access the object of Singleton class class Test2 { public static void main(String[] args) { Test1 t = Test1.getTest(); } } Output : Approach 2 executed successfully.
Note : 1. In Approach-1(A1) object get created at the beginning. If we are not using the object in that case memory wastage occur. 2. Approach-2(A2) is recommended in comparison of A1 because in A2 object get created only when it requested to do so and after first request for every new request it returns the existing object that is created at first request.
Thanks

Singleton class in Java

  • 1.
  • 2.
    What is Singletonclass..? If we can create only one object of java class then that java class is said to be Singleton class. OR Singleton class is a class that can have only one object at a time. for e.g. Runtime BusinessDelegate
  • 3.
    Why use Singletonclass… If there are several similar requirement and we have create only one object and reuse the same object for every similar requirement. The purpose of singleton is to control object creation by keeping private constructor. The main advantage of singleton class is improvement of performance and memory utilization.
  • 4.
    e.g. Runtime r1 = Runtime.getRuntime(); Runtimer2 = Runtime.getRuntime(); . . . . . Runtime rn = Runtime.getRuntime(); Object r1 r2 rn Note : We can create singleton class objects by using factory methods. As we can see that for n number of references only one object has been created.
  • 5.
    Can we createour own singleton classes..? Yes, we can create our own singleton classes. To create the user-defined singleton classes we have to use private constructor, private static variable and public factory method. There are two approaches to create singleton class
  • 6.
    Approach 1 : Step1: Create a Singleton class class Test1 { private static Test1 t = new Test1(); private Test1() { System.out.println("Approach 1 executed successfully."); } public static Test1 getTest() { return t; } } Step 2: Access the object of Singleton class class Test2 { public static void main(String[] args) { Test1 t = Test1.getTest(); } } Output : Approach 1 executed successfully.
  • 7.
    Approach 2 : Step1: Create a Singleton class class Test1 { private static Test1 t = null; private Test1() { System.out.println("Appro ach 2 executed successfully."); } public static Test1 getTest() { if(t == null) { t = new Test1(); } return t; } } Step 2: Access the object of Singleton class class Test2 { public static void main(String[] args) { Test1 t = Test1.getTest(); } } Output : Approach 2 executed successfully.
  • 8.
    Note : 1. InApproach-1(A1) object get created at the beginning. If we are not using the object in that case memory wastage occur. 2. Approach-2(A2) is recommended in comparison of A1 because in A2 object get created only when it requested to do so and after first request for every new request it returns the existing object that is created at first request.
  • 9.