Skip to main content

Can Constructors be Private ?



Yes, a constructor can be private. A private constructor can be useful in many ways.


  • It prevents instantiation outside the object. If we make the constructor private then it can only be accessed inside that class. For example, in the following software design patterns we can use it: 
           - Singleton Patter
           - Factory Pattern

  • It prevents sub-classing.  If we make an private constructor, no other classes can extend that class. It's kind of using the final  keyword.

 Lets, look at the code of a Singleton Pattern:


Comments