FAQ

Java

JSP

Servlet


Advertisement



When to use "private" constructor?

The private constructors are used in the following situation:

  • When you implement singletons or factory design pattern.
  • Do not want people to instantiate your class directly. e.g., new MyObject();. For example, all of you method and fields are static, you may need to add a private default constructor (if you do not have it, compiler will add one for you that is not what you want). Such way can enforce that people can not accidently use "new".


Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend

Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |