DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Java Virtual Threads and Scaling
  • Java’s Next Act: Native Speed for a Cloud-Native World
  • The Energy Efficiency of JVMs and the Role of GraalVM
  • Understanding Root Causes of Out of Memory (OOM) Issues in Java Containers

Trending

  • A Complete Guide to Modern AI Developer Tools
  • Building Resilient Networks: Limiting the Risk and Scope of Cyber Attacks
  • Simplifying Multi-LLM Integration With KubeMQ
  • Automating Data Pipelines: Generating PySpark and SQL Jobs With LLMs in Cloudera
  1. DZone
  2. Coding
  3. Java
  4. How to Use Singleton Design Pattern in Java

How to Use Singleton Design Pattern in Java

There are several ways to use the singleton design pattern. Check out this post on how to use the singleton design pattern through four different methods.

By 
Brijesh Saxena user avatar
Brijesh Saxena
DZone Core CORE ·
Updated Jul. 20, 18 · Tutorial
Likes (38)
Comment
Save
Tweet
Share
223.8K Views

Join the DZone community and get the full member experience.

Join For Free

We have various ways of creating singletons in Java. Now, first of all, what is Singleton and why is it required?

The singleton design pattern is used to restrict the instantiation of a class and ensures that only one instance of the class exists in the JVM. In other words, a singleton class is a class that can have only one object (an instance of the class) at a time per JVM instance. There are various ways to design/code a singleton class.

  1. Class-level Member (Eager Initialization Method): 
    1. Make constructor private.
    2. Make a private constant static instance (class-member) of this Singleton class.
    3. Write a static/factory method that returns the object of the singleton class that we have created as a class-member instance.
    4. We can also mark a static member as public to access constant static instance directly. But, I like to access class/instance members via methods only.
    5. So, the singleton class is different from a normal Java class in terms of instantiation. For a normal class, we use a constructor, whereas for singleton class we use the getInstance()method.
  2. public class SingletonClass {
        private static final SingletonClass SINGLE_INSTANCE = new SingletonClass();
        private SingletonClass() {}
      public static SingletonClass getInstance() {
          return SINGLE_INSTANCE;
        }
    }
  3. Class-level Member (Lazy Initialization Method):
    1. Make constructor as private.
    2. Make a private static instance (class-member) of this singleton class. But, DO NOT instantiate it.
    3. Write a static/factory method that checks the static instance member for null and creates the instance. At last, it returns an object of the singleton class.
  4. public class SingletonClass {
        private static SingletonClass SINGLE_INSTANCE = null;
        private SingletonClass() {}
        public static SingletonClass getInstance() {
            if (SINGLE_INSTANCE == null) {  
              synchronized(SingletonClass.class) {
              SINGLE_INSTANCE = new SingletonClass();
              }
            }
            return SINGLE_INSTANCE;
        }
    }

  5. Class-level Member (Lazy Initialization with double lock Method):
    1. Here, we run into a problem. Suppose that there are two threads running. Both can get inside of the if statement concurrently when the instance is null. Then, one thread enters the synchronized block to initialize the instance, while the other is blocked. When the first thread exits in the synchronized block, the waiting thread enters and creates another singleton object. Note that when the second thread enters the synchronized block, it does not check to see if the instance is non-null.
  6. public class SingletonClass {
        private static SingletonClass SINGLE_INSTANCE = null;
        private SingletonClass() {}
        public static SingletonClass getInstance() {
            if (SINGLE_INSTANCE == null) {
                synchronized (SingletonClass.class) {
                    if (SINGLE_INSTANCE == null) {
                        SINGLE_INSTANCE = new SingletonClass();
                    }
                }
            }
            return SINGLE_INSTANCE;
        }
    }
  7. By using nested Inner class (Lazy Load method):
    1. In this method is based on the Java Language Specifications (JLS). Java Virtual Machine loads static data-members only on-demand. So, here the class SingletonClass loads at first by the JVM. Since there is no static data memberin the class; SingletonClassHolder does not loads or creates SINGLE_INSTANCE.
    2. This will happen only when we invoke getIntance method. JLS guaranteed the sequential execution of the class initialization; that means thread-safe. So, we actually do not need to provide explicit synchronization on static getInstance() method for loading and initialization. Here, since the initialization creates the static variable SINGLE_INSTANCE in a sequential way, all concurrent invocations of the getInstance() will return the same correctly initialized SINGLE_INSTANCE without synchronization overhead.
  8. public class SingletonClass {
        private SingletonClass() {}
        private static class SingletonClassHolder {
            static final Something SINGLE_INSTANCE = new SingletonClass();
        }
        public static SingletonClass getInstance() {
            return SingletonClassHolder.SINGLE_INSTANCE;
        }
    }
  9. By using Enums:All of the above approaches are not full-proof in all the cases. We can still create multiple instances of the above implementations by using serialization or reflection. In both of the cases, we can bypass the private constructor and, hence, can easily create multiple instances. So, the new approach is to create singleton class by using enums since enums fields are compiled time constants, but they are instances of their enum type. And, they're constructed when the enum type is referenced for the first time.
public enum SingletonClass {
    SINGLE_INSTANCE;
}


FAQs:

  1. How to prevent cloning of Singleton object? Well, we can throw an exception from the clone method of the Singleton class.

  2.  How to prevent duplicate instance via Serialization/Deserialization of Singleton? We have to override/implement the readobject() method and have to return the same singleton instance of the class. Otherwiase, Implement SingletonClass as enum; That's the best way for thie case.  

public enum SingletonClass {
    SINGLE_INSTANCE;
}
Java (programming language) Java virtual machine Design

Opinions expressed by DZone contributors are their own.

Related

  • Java Virtual Threads and Scaling
  • Java’s Next Act: Native Speed for a Cloud-Native World
  • The Energy Efficiency of JVMs and the Role of GraalVM
  • Understanding Root Causes of Out of Memory (OOM) Issues in Java Containers

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!