Sunday, 14 January 2018

Java Singleton Highlghts



Singleton classes ensure that only one instance of the class exists in one JVM.

How to achieve this?

  • Use private constructor to restrict instantiation of the class from other class
  • Use private static variable of the same class that will be the only one instance of the class
  • Use private static method to returns the instance of the class , this will be global access point to access the instance.

What are the ways we can create Singleton class?

  • Eager Initialization
  • Static block initialization
  • Lazy Initialization
  • Thread safe implementation