What is Singleton class?
Objects are created in run-time and are allocated in a memory area in JVM known as the
heap. There may be many objects of a single class. In some cases, we will want to have
one and only one instance of a class in the JVM. Such a class is said to be a singleton class.
To design a singleton class
• We need to declare the reference variable of the class as private and static.
• We need to make the default constructor of the class as private. In this way we can ensure that no other class can create an instantiate the object of this class outside the class body.
• We provide a static member with a null check condition to access the one and only one instance of the class.
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment