VOOZH about

URL: https://www.geeksforgeeks.org/java/singleton-class-java/

⇱ Singleton Method Design Pattern in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Singleton Method Design Pattern in Java

Last Updated : 18 May, 2026

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.

  • Allows only one object of the class to be created
  • Provides global access to the same instance
  • Helps save memory by reusing the same object

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.

Purpose of Singleton Class

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.

  • Memory Efficient: The object is created only once and reused whenever needed, reducing memory usage and object creation overhead.
  • Resource Control: Helps manage shared resources efficiently in applications such as caching, logging, thread pooling, and database connectivity.
  • Thread Safety: Ensures that only one thread or one connection accesses a shared resource at a time in multi-threaded environments.

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.

Forms of Singleton Class Pattern

There are two forms of singleton design patterns, which are:

  • Early Instantiation: The object is created at the time of class loading.
  • Lazy Instantiation: The object is created only when it is required for the first time.

Creating a Singleton Class in Java

To create a Singleton class in Java, follow these steps:

  • Create a private constructor to prevent direct object creation from outside the class.
  • Declare a private static variable to store the single instance of the class.
  • Create a public static method (commonly getInstance()) that returns the same instance every time using the Lazy Initialization approach.
  • This ensures that only one object of the class is created throughout the application.

Example 1: Singleton class using the getInstance() method.


Output
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 class

The 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.


Output
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.

Normal Class vs Singleton Class

FeatureNormal ClassSingleton Class
Object CreationMultiple objects can be createdOnly one object is created
Constructor AccessUsually publicConstructor is private
Instance AccessObjects created using new keywordObject accessed using getInstance() method
Memory UsageMore memory used due to multiple objectsMemory efficient because a single object is reused
Object ReferenceEach object has a different referenceAll references point to the same object
PurposeUsed for general object creationUsed for shared resources like logging, caching, database connection
ExampleStudent s1 = new Student();Singleton s = Singleton.getInstance();
Comment
Article Tags:
Article Tags: