26. 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.
  • Used in the singleton pattern. (Refer Q45 in Java section).
  • Used in the factory method pattern (Refer Q46 in Java section).
  • Used in utility classes e.g. StringUtils etc.

No comments:

Post a Comment