when we create singleton design then we create private
constructtor..so how JVM take private constructor to make
object..but it's private..



when we create singleton design then we create private constructtor..so how JVM take private constr..

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

Post New Answer

More Core Java Interview Questions

What is break and continue statement?

0 Answers  


what is an object class?Explain its methods?

6 Answers  


What is the output of the following Java program? class Main { public static void main(String args[]){ final int i; i = 10; System.out.println(i); } } 10. What is the output of the following Java program? class Main { public static void main(String args[]){ final int i; i = 10; System.out.println(i); } }

1 Answers  


What is lifetime variable?

0 Answers  


How can we find the sum of two linked lists using stack in java?

0 Answers  


What is a treeset in java?

0 Answers  


Explain JSP life cycle

1 Answers   Cap Gemini,


What modifiers are used with a top level class?

3 Answers  


What is string immutability?

0 Answers  


Write a program to print fibonacci series up to count 10.

0 Answers  


Why chararray() is preferred over string to store the password?

0 Answers  


Why singleton pattern is better than creating singleton class with static instance?

0 Answers  


Categories