-5

The static inner class can guard against momory leak?

Du.biqi
  • 3
  • 1
  • just because something is a static inner class doesn't make it a singleton. what's your point? – Stultuske Jul 25 '16 at 06:24
  • Possible duplicate of [What is an efficient way to implement a singleton pattern in Java?](http://stackoverflow.com/questions/70689/what-is-an-efficient-way-to-implement-a-singleton-pattern-in-java) – Alex K Jul 25 '16 at 07:46

1 Answers1

0

According to Joshua Bloch's "Effective Java" book, the best (and maybe easiest) way to create a Singleton is to make a single-element enum:

public enum Singleton {
    INSTANCE;

    Singleton() { }
}

This way it's guaranteed that:

  • the Singleton is thread-safe
  • single instance will be created for the application lifespan
  • the instance is implicitly Serializable.

More info:

Community
  • 1
  • 1
Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147