my dog learned polymorphism
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why private constructor? 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why private constructor?" Watch "Why private constructor?" New topic

Why private constructor?

Louie van Bommel
Ranch Hand

Joined: Aug 17, 2004
Posts: 76
Over a month ago I read something about a use for a private constructor. But now I can't even imagine one. I think the example cited made the class final as well as making the constructor private.

Question 1: What's the point of making a constructor private if the class if final?

Question 2: Anyone have a good (example) piece of code where it'd be useful to make a constructor private?
Kathy Sierra
Cowgirl and Author

Joined: Oct 10, 2002
Posts: 1589
You could make a constructor private to prevent any outside code from directly instantiating that class. Some people use this as a form of implementing a Singleton--they make a static method like getTheObject(), and it returns the one and only one instance of that class (the first time the method is invoked, the object is created and the new object is returned; after that, the one existing object is returned regardless of how often the method is called. The point is that marking the constructor private prevents anyone from ever saying "new TheObject()", so that you have complete control over instantiation of that class, and complete control over the object(s) of that type that are allowed to be used.

fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11882

I believe the Math class has a private constructor. You cannot make a Math object. However, since all it's methods are static, you can access them without needing to create one.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Tom Tolman
Ranch Hand

Joined: Sep 02, 2004
Posts: 83
You could make a constructor (among many) which you do not wish exposed- but you wish to be able to call from within its own alternate constructor or within the class itself.

You could imagine a class which could use instances of itself which are created in a specific way- perhaps with only part of the data structure utilized and which should not be allowed to be created by anything other than the implementation of the class itself.
Prasanna Phatak

Joined: Sep 06, 2004
Posts: 7
As fred says Math is a right example of the Private constructor class.

The perfect example of Singlton class (having Private constructor) I have seen is:

A Logger class.
Consider a huge application and every object in a class requires to log the activities,
If every Class makes an instance of the Logger class then unnecesarily the objects will get created.

So you can make a Logger class Singleton, which will be instantited once, in the whole life of a program. And will remain live for the whole life of a program.

So: What I found is that.
You can make class constructor private for following reason.
1) Not to allow the user to instantiate a class.
[ September 07, 2004: Message edited by: Sampras ]

Thanks,<br />Prasanna.
I agree. Here's the link:
subject: Why private constructor?
It's not a secret anymore!