Where and how can you use a private constructor?

Private constructor is used if you do not want other classes to instantiate the object and to prevent subclassing. The instantiation is done by a public static method (i.e. a static factory method) within the same class.

  • Used in the singleton design pattern.
  • Used in the factory method design pattern e.g. java.util.Collections class
  • Used in utility classes e.g. StringUtils etc.
Tagged . Bookmark the permalink.

Leave a Reply