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

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.