![]() |
VOOZH | about |
Singleton Design Pattern in Java ensures that a class has only one instance throughout the application and provides a global access point to it. It is mainly used when a single shared object is required, such as database connections, logging, or configuration settings. The constructor is made private to prevent creating multiple objects from outside the class.
Note: While designing a Singleton class, make the constructor private and provide a static method that returns the object of the class using the Lazy Initialization technique.
The main purpose of a Singleton class in Java is to ensure that only one object of the class is created during the entire application lifecycle. It provides a single global access point to shared resources like database connections, sockets, caches, and configuration settings.
Example of singleton classes is Runtime class, Action Servlet, and Service Locator. Private constructors and factory methods are also an example of the singleton class.
There are two forms of singleton design patterns, which are:
To create a Singleton class in Java, follow these steps:
Example 1: Singleton class using the getInstance() method.
Hashcode of x is 1995265320 Hashcode of y is 1995265320 Hashcode of z is 1995265320 Three objects point to the same memory location on the heap i.e, to the same object
Explanation: In the above program, the getInstance() method is used to create the Singleton object. When it is called for the first time, it creates the object single_instance; after that, the same object is returned every time instead of creating a new one.
👁 Singleton classThe variable single_instance is declared as static, so it is shared among all objects of the class. That is why objects x, y, and z have the same hashcode, which shows that all three references point to the same object in memory.
Example 2: Singleton class with the same method name as class.
String from x: STRING FROM SINGLETON CLASS, y: STRING FROM SINGLETON CLASS String from x: string from singleton class, y: string from singleton class
Explanation: In the singleton class, when we first-time call Singleton() method, it creates an object of class Singleton with the name single_instance and returns it to the variable. Since single_instance is static, it is changed from null to some object. Next time if we try to call Singleton() method, since single_instance is not null, it is returned to the variable, instead of instantiating the Singleton class again.
| Feature | Normal Class | Singleton Class |
|---|---|---|
| Object Creation | Multiple objects can be created | Only one object is created |
| Constructor Access | Usually public | Constructor is private |
| Instance Access | Objects created using new keyword | Object accessed using getInstance() method |
| Memory Usage | More memory used due to multiple objects | Memory efficient because a single object is reused |
| Object Reference | Each object has a different reference | All references point to the same object |
| Purpose | Used for general object creation | Used for shared resources like logging, caching, database connection |
| Example | Student s1 = new Student(); | Singleton s = Singleton.getInstance(); |