OK, Singleton in Java.

Fine, this topic has been talked many many times, and you can find a lot of great answers over the internet. Stackoverflow.com listed a lot of answers. And also, you can find great approach from Joshua Bloch’s Effective Java also pointed out 2 effective approaches. What’s more, Joshua Bloch also provided an enum approach(Page 31) as the best practice for Serializable Singleton. If you like Wikipedia, you can also find a Java solution for Singleton, and for deep background knowledge behind this approach, please read “Double check locking” and “Java 5 memory management” by Bill Pugh.

If you are as lazy as me, just remember the following implementation, which is thread safe and can be used on all the java version. The key point in this implementation is using Java atomic class load mechanism to guarantee the thread safe and using static method to implement lazy initialization.

If you also have some curious on the other implementations, please keep reading.

  1. The simplest implementation(Immutable class, thread safe, but no lazy initialization)
  2. The double check locking implementation(Thread safe, lazy initialization, but requires Java 5+)
  3. The enum implementation(Thread safe, Serializable, also requires Java 5+)

After reading all of these, I don’t think you need any other implementations, but to understand why and how these implementations work, you need read something more[Typically those links I provided at the beginning]. There are things called Google and Bing on the earth, I guess you can find the answer very soon.