Singleton design pattern is a kind of design pattern which helps a java developer to apply restriction to a class to have one and only one instance. This way the developer can ensure as well as can control how an object is instantiated and can prevent others from creating or copying multiple instances of the class.
Purpose: The main purpose is that if we have to use a resource then we can create a singleton class which can only access the resource and by making this instance available to other threads or any part of the application we can make sure that unnecessary objects are not getting created for accessing the resource.
Another purpose is that we have some utility classes which are being used by other java classes to do some calculations etc. Now if we make the utility class a singleton class then all objects and all parts of the application can use this one instance of the utility class and no extra instaces are required. There are lots of other uses also.
Steps to create Singleton:
1. Make the constructor private-
The default constructor of the class needs to be made private. Then other classes can instantiate any object of the class by using the new keyword. The new keyword can then be used from within the class only.
E.G.-
public class SingletonDemo{ private SingletonDemo() {} }
2. One method to get the instance- Though we’ve made the constructor private so that other classes can’t instantiate but we should have one method to get the only one instance.
E.G.-
public class SingletonDemo { private static SingletonDemo instance; private SingletonDemo() {} public static synchronized SingletonObject getInstance() { if instance == null) { instance = new SingletonDemo(); } return instance; } }
In the above example, we have the method getInstance() which is providing the one and only one instance of the class. First it is checking if the object is instantiated or not(by checking if it is null or not). If it is not done yet then it creates a new instance. The instance is made static because only one class level instance variable will be there. The method is made synchronized to make it thread safe.
3. Still we can create instances of the class by cloning the class. So we have to override the clone() method of the Object class so that if any attempt is made to clone the class it will throw a CloneNotSupportedException.
The complete code is then:
public class SingletonDemo{ private static SingletonDemo instance; /** A private Constructor prevents any other class from instantiating. */ private SingletonDemo() {} public static synchronized SingletonObject getInstance() { if instance == null){ instance = new SingletonDemo(); } return instance; } public Object clone() throws CloneNotSupportedException{ throw new CloneNotSupportedException(); } }
Application-
public class SingleTonApp { public static void main(String args[]) { SingletonDemo obj = SingletonDemo.getInstance(); System.out.println(”Singleton object obtained”); } }
Related Articles:
- JfreeChart Series-3: How to create a dynamic 3D Pie Chart using JFreeChart library in Java You may refer to the earlier tutorials if you have...
- JfreeChart Series-2: How to create a dynamic Pie Chart using JFreeChart library in Java In the earlier tutorial I showed you how to create...
- Java Comparator example Sometimes we need to sort objects based on certain attributes...
- Reading user input in java Sometimes in our stand alone java program we need to...
- How to create Windows executable EXE from a Java Jar Creating Windows executable EXE file from a Jar file: //...
- JfreeChart Series-1: How to create a dynamic Bar Chart using JFreeChart library in Java In Java creating any graph or chart like bar chart...
- How to load a properties file in Java This tutorial will explain how to access properties file through Java...
- How to test if a String is palindrome or not in Java? Steps to check if a given string is a Palindrome...
- creating non duplicate array from duplicate array in Java This example helps you to generate a fresh and non...
You can subscribe by e-mail to receive news updates and breaking stories.