2017  Kodetalk | Feedback | Privacy Policy | Terms | About
userimage

Can constructor declared as private?

can this be done,please suggest.
userimage

Constructor can use any access modifier even private also.If a constructor is declared private in a class then it will create only one object to its class ,that type of classes are called Singleton classes.

SingleTon class code :

class Singleton{

private static SingleTon st =null;

private SingleTon()

{

System.out.println("n this is constructor");

}

public static SingleTon stfactory()

{

if(st==null)

st=new SingleTon();

return st;

}

}










Answer is