Static Class Vs SingleTon Pattern

WoW ! Both have their own usage but still investigation is required - which one to use ?

  • Singleton is more object oriented.
  • Singleton can use inheritance & polymorphism to extend a base class, implement an interface. E.g. java.lang.Runtime is a singleton class but call to get Runtime () returns different implementation based on JVM. If this is made static, it would be difficult to return different implementation.
  • Static class cannot inherit their instance members.
  • Singleton can be initialized lazily or asynchronously but static class is first loaded.
  • Static classes do make sense:
    • If class is just providing global access to methods & not maintaining any state. This is high performance as compared to singleton class as static binding occurs at compile time.
    • E.g. java.lang.Math is a static class with all static methods.

ARCHITECTURES · DESIGN-PATTERNS
architectures design-patterns static class vs singleton singleton static class

You May Also Enjoy

Liskov Substitution Principle

Static Class Vs SingleTon Pattern

Layer Wise Design Patterns

Static Class Vs SingleTon Pattern

Chain of Responsibilty

Static Class Vs SingleTon Pattern

Issues With Caching

Static Class Vs SingleTon Pattern


Dialogue & Discussion