when we create singleton design then we create private
constructtor..so how JVM take private constructor to make
object..but it's private..
Answer Posted / 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 |
Post New Answer View All Answers
How would you dynamically allocate memory to an array?
Is java map thread safe?
What does nextint () do in java?
Define how can we find the actual size of an object on the heap?
When we should use serialization?
What is serialization in java?
How to split arraylist elements in java?
What is the difference between length and size in java?
What are predefined functions?
Why do I need to declare the type of a variable in java?
List two java ide’s?
What is the basic of java?
Can you override static methods?
What do you mean by hashing?
How hashmap increases its size in java?