A private constructor can be called outside its class but how??????
We can not make the instance of the private constructors. But someone told me that a private constructor can be called.how???????
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sukesh MarlaPosted Aug 21, 2012, 2:22 AM
You cant invoke private constructor from Outside the clas.
We declare constrcutor as private when we want to restrict user from creating objects..
Normallyin this case we create static method in class which will return the Class Object
public class A
{
private A()
{
}
public static A GetA()
{
return new A();
}
}
.
.
.
.
Main()
{
A a=A.getA();
}
Hope this helped, check this is correct answer
Santhosh Kumar JayaramanPosted Aug 21, 2012, 1:01 AM
http://www.ipreferjim.com/2011/08/c-instantiating-an-object-with-a-private-constructor/