Home  |   STIU  |   WOW  |   SCJP  |   SCDJWS   |   JEE FAQ  |   About US  |  

FAQ
  Java FAQ
  JSP FAQ
  Servlet FAQ
 

Advertisement

XyzWs Java FAQ:
When to use private constructor?


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
Advertisement
XyzWs Java FAQ : When to use "private" constructor?

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".

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

Support  | Feedback  | Help