Monday, August 07, 2006

Where and how can you use a private constructor?

Private constructor is used if you do not want other classes to instantiate the object. The instantiation is done by a public static method within the same class.
1. Used in the singleton pattern. (Refer Q45 in Java section).
2. Used in the factory method pattern (Refer Q46 in Java section).
3. Used in utility classes e.g. StringUtils etc.


Post a Comment

<< Home