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
Is singleton set an interval?
How many arguments can a method have java?
How would you format a date in java? I.e. In the ddmmyyy format?
What is meant by overloading?
What is array length in java?
Write a program to solve producer consumer problem in java?
What do you mean by global variable?
What is intern method in java?
Explain about doubly linked list
What is the difference between Java and C++?
How do you use compareto in java?
Can I learn java in 3 months?
Which is dependent variable?
How are multiple inheritances done in Java?
please send me hr interview questions in it industry