This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About Private Constructors

 
Hareendranath Babu Kotha
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the need of Private constructors ?
 
Kaush Kane
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One use of private constructors that I know is while creating a singletone instance of a class. This is implemented as shown below:

class A{
private static A instance = null;
private int var;
private A() // private constructor
{
var = -1;
}
/* Getting singleton instance of class A */
public static A getInstance()
{
if(instance == NULL)
{
instance = new A()
}
return instance
}
}

 
Ramen Chatterjee
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

There has just been a discussion about just this in the previous thread.

Ramen
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic