🔙

the perfect singleton

This article is not about what the singleton is and even not about design patterns. Today, I talk on what is the best implementation of this admirable pattern.

To be honest, I used to think that double-check is the best choose. However, after reading the article on java memory model I realized that the world would be never the same. It turns up, volatile is not cheap, and it is barely better than synchronized block. According to this fact, if you need lazy initialization and thread-safe implementation, you should use the approach below:

public final class Singleton {
      private Singleton(){};
      
      public static Singleton getInstance(){
          return SingletonCreator.INSTANCE;
      }
      
      private static class SingletonCreator {
          private static final Singleton INSTANCE = new Singleton();
      }      
}

This code is lazy, because class SingletoneCreator is not initialized until it is not needed and not used in other places of program. But there is only one place where this class might be used. This place is a method getInstance.

This code is thead safe because the field INSTANSE is final and java memory model guarantees us that all final fields have been initialized only once and will be consistent for all threads.