permaculture playing cards
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
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
jQuery in Action, 3rd edition