This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes About Private Constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "About Private Constructors" Watch "About Private Constructors" New topic

About Private Constructors

Hareendranath Babu Kotha
Ranch Hand

Joined: May 22, 2006
Posts: 40
What is the need of Private constructors ?

Hari.K,<br />+919886082889
Kaush Kane
Ranch Hand

Joined: May 22, 2006
Posts: 37
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

Joined: Apr 27, 2006
Posts: 62

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


Could try harder
I agree. Here's the link:
subject: About Private Constructors
It's not a secret anymore!