Monday, March 9, 2009

Private Constructor Myth



This is useful in two situations:


·         If your class serves only as a container for some static members or properties, and therefore should never be instantiated. But here  I believe in such case better would be to make the class itself as ‘Static’, instead of keeping the normal constructor and making it ‘Static’.

·         If you want the class to only ever be instantiated by calling some static member function (this is the so-called class factory approach to object instantiation)

·         Helpful in Singleton implementation – Where you want to control only one Instance Creation.

·         Does not forces to make any member variable[ required to be referred] in constructor to be static.



Arun Manglick

No comments:

Post a Comment