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

How many bytes is a unicode character?

0 Answers  


What are the two types of java?

0 Answers  


Is alive in java?

0 Answers  


Why super is first line in java?

0 Answers  


Why deletion in linkedlist is fast than arraylist?

0 Answers  






Write a program to show whether a graph is a tree or not using adjacency matrix.

0 Answers   Amazon,


In Inheritance if we are implementing Multi level inheritance and all class having same name of variable and now i want to access each class variable and how it is possible?

2 Answers  


What are pass by reference and pass by value?

0 Answers  


What is void data type?

0 Answers  


What an i/o filter in java programming?

0 Answers  


State one difference between a template class and class template.

0 Answers   HAL,


What is array command?

0 Answers  


Categories