When should I use a "private" constructor?

John Mitchell

When you don't want anyone outside the class to invoke that particular constructor.

The most common use that I see for private (and protected) constructors is in implementing singletons and some types of object factories. Basically, you use a static method to manage the creation of one (or more) instances of the class.

0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact