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 the purpose of finalization in java programming?

0 Answers  


all are saying java doesn't support multiple inheritance but by default Object class is super class for all the user defined classes and we can extend atmost one class so each class can extend more than one class so java supports multiple inheritance?i am confused with this,pls any one explain me.

4 Answers  


in a console we r giving java <class name> if r pressing enter where it'll goes

2 Answers   CSC,


What is design pattern and there types?

2 Answers   Cap Gemini,


why the wait,notify,notifyall methods are placed in object class?these are the thread concepts why these methods are placed in Object class?

2 Answers   Global Logic, Satyam,






Explain the concept of hashtables?

0 Answers  


What is args length in java?

0 Answers  


When try and catch block is used ?

6 Answers  


What is a package in java? List down various advantages of packages.

0 Answers  


What is parsing a string?

0 Answers  


What are the important features of Java 11 release?

0 Answers  


Hi ,i convert contrller as jSp And presentation as servlet ...will it do? if so what are advantage and idsadvantages

2 Answers   IBM,


Categories