Explain private constructor in java java interview question interviewdot

preview_player
Показать описание

Explain private Constructor in Java ?

Constructors can use any access modifier, including private.

A private constructor means only code within the class itself can instantiate an object
of that type, so if the private constructor class wants to allow an instance of the class to be used, the class must provide a static method or variable that allows access to an instance created from within the class.
Public Class PrivateConstDemo {

private PrivateConstDemo (){ // no other class can instantiate.
}

Public synchronized static PrivateConstDemo getInstance(){
if(demo == null){
demo = new PrivateConstDemo();
}
}
}
A Job Portal

Other Top Job Portals

Are you looking for a new Job ?
Рекомендации по теме
join shbcf.ru