Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regarding static holder singleton pattern

Tags:

java

I have developed singleton in many ways depending upon the condition like volatile/lazy singleton, eager singleton, normal singleton and through Enum also, but specifically I want to know about static holder pattern singleton shown below.

public static class Singleton {
    private static class InstanceHolder {
        public static Singleton instance = new Singleton();
    }

    private Singleton(){}

    public static Singleton getInstance() { 
        return InstanceHolder.instance;
    }
}

Please advise under which conditions it is beneficial and what are its benefits.

like image 445
user2094103 Avatar asked Feb 22 '13 07:02

user2094103


People also ask

Why does singleton pattern have static?

Singletons have a static property that you must access to get the object reference. So you are not instantiating an object such as in the manner we do for a normal class.

Can a singleton class have static variables?

Singleton implementation can either have static members or instance members. Static classes can contain static members only. It can implement any other interface or base class is required. It cannot implement the interface or any other base class.

Can a singleton have static methods?

A singleton doesn't use static methods, so you won't have trouble using it in a non-static context. Singletons can be extended/subclassed. Since they're objects, they can be injected into other objects, which allow for the creation of some great design patterns utilizing the concepts of dependency injection.

What is singleton pattern explain with example?

In Java, Singleton is a design pattern that ensures that a class can only have one object. To create a singleton class, a class must implement the following properties: Create a private constructor of the class to restrict object creation outside of the class.


2 Answers

This pattern is beneficial for at least 3 reasons:

  1. Static factory
  2. Lazy initialization
  3. Thread safe

The JVM defers initializing the InstanceHolder class until it is actually used, and because the Singleton is initialized with a static initializer, no additional synchronization is needed. The first call to getInstance by any thread causes InstanceHolder to be loaded and initialized, at which time the initialization of the Singleton happens through the static initializer.

Static holder pattern is also considered as the smartest replace for Double-check-locking antipattern.

like image 105
Vladimir Kishlaly Avatar answered Oct 21 '22 22:10

Vladimir Kishlaly


This is a way to make a thread-safe lazy singleton by exploiting the way how JVM loads classes. You can read more about why and how to correctly implement it in Bloch's Effective Java book.

Remember, that from the testable code point of view singletons (and global state in general) are not beneficial and should be avoided.

like image 7
denis.solonenko Avatar answered Oct 21 '22 20:10

denis.solonenko