Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP 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: 13071
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