DZone
Java Zone
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
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > The Singleton Pattern and its Simplest Implementation in Java

The Singleton Pattern and its Simplest Implementation in Java

Axel Rauschmayer user avatar by
Axel Rauschmayer
·
Apr. 11, 11 · Java Zone · Interview
Like (0)
Save
Tweet
11.40K Views

Join the DZone community and get the full member experience.

Join For Free

This post motivates the Singleton design pattern and explains its implementation in Java.

The Singleton is a design pattern [1] to ensure that a class has only a single instance. The name is derived from the mathematical term singleton which means a set with exactly one element. This pattern is mainly used if a class implements a service (which is usually instantiated only once). A typical Singleton looks as follows:

    public class Singleton {
 
        public static final Singleton INSTANCE = new Singleton();
 
        // Private constructor prevents external instantiation
        private Singleton() {
        }
    }
Making the constructor private prevents code that is external to the class from instantiating it. This is done for two reasons:
  • Enforce the constraint of exactly one instance.
  • Hint at how this class is to be used: To get an instance, one doesn’t invoke the constructor, but accesses the static variable. Thus, one cannot accidentally misuse the class. Classes with factory methods have private constructors for the same reason.
And now for the simplest way of implementing a Singleton in Java: as an enum.
    public enum Singleton {
       INSTANCE;
       
       // Instance variables and methods go here
    }
The enum automatically ensures that the Singleton cannot be instantiated externally, and the enum constant INSTANCE is a convenient way of creating the only instance.

As an alternative to the above solutions, one could also use static methods and store internal state in static variables. But then one loses some of the amenities of instances, such as the ability to conform to an interface. Or the ability to pass around a reference and invoke methods. It should be noted that the Singleton pattern hampers testability, because Singletons are harder to replace in a test environment. If you want to implement services (or components) via classes, it is better to use dependency injection (e.g. via Guice [3]). In addition to increased configurability, you also get explicit dependencies stated in constructors.

Related reading:

  1. The classic book on design patterns, recommended: “Design Patterns: Elements of Reusable Object-Oriented Software” by Erich Gamma, Richard Helm, Ralph Johnson, John M. Vlissides.
  2. The Wikipedia article on the Singleton pattern (which inspired this post and from which the enum implementation is taken).
  3. Google Guice, a lightweight dependency injection framework for Java. The website also explains dependency injection well.

 

From http://www.2ality.com/2011/04/singleton-pattern-and-its-simplest.html

Singleton pattern Java (programming language) Implementation Dependency injection

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • SQL GROUP BY and Functional Dependencies: a Very Useful Feature
  • How to Build Microservices With Node.js
  • Creating an Event-Driven Architecture in a Microservices Setting
  • What's the Difference Between Static Class vs. Singleton Patterns in C#?

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo