What is use of private constructors & when should we create them?

There are two points about Private Constructor.

First, If a class has only private constructor & no public constructor, then it can not be instantiated by any other class except it's nested class. It means private constructor prevents other class to create its instance

Secondly, the private constructor is used in classes that contain only static members.

For example, we use private constructor in the Singleton pattern. Singleton pattern satisfies both of the above conditions.

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. For more information - please visit our private policy.