when we create singleton design then we create private
constructtor..so how JVM take private constructor to make
object..but it's private..
Answer / pals
-Singleton design implies only 1 instance of a class can exist
-Private constructor implies only that class's member variables can call the constructor(Create an object)
-So a singleton class will have a private constructor only when you want to create ONLY 1 instance of the class without giving any other class the opportunity to create the class's objs using "new"; so you will have to write a static method in which u create an instance of this class and using this static method expose the reference of this object to the calling classes.So this will make sure that every user will use the same instance of the class.
Summary: JVM lets ONLY the private constructor class's members create only 1 obj of this class and lets other uses use this obj only by exposing a public static method.
I hope this is clear :)
| Is This Answer Correct ? | 21 Yes | 0 No |
Difference between ?System.out.println? and ?System.error.println??
Can a function return a function?
what is mutual exclusion? : Java thread
What is type conversion in java?
What is high level language in computer?
If an application has multiple classes in it, is it okay to have a main method in more than one class?
Is map sorted in java?
What are the different types of data structures in java?
What are different types of control structures?
Convert Binary tree to linked list.
What is variable and constant explain with example?
How do you escape a string?