Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why are constructors declared private. Also is there any other way to achieve this

 
Chiran Mathur
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why should this be done? How can declaring a constructor private make it have only instance?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When all constructors are private, you can only get an instance through a static method of the class. That static method can manage a pool of instances or ensure that only one instance is created.
Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic