jQuery in Action, 2nd edition*
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why private constructor?" Watch "Why private constructor?" New topic
Author

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
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
Howdy,
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.

Cheers,
Kathy
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11154
    
  16

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
Greenhorn

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Why private constructor?
 
Similar Threads
what will happen if we make a constructor private?
inheritance problem
Constructors and Access Modifiers
significance of private with final modifier method
Implements Vs Extends on a constants file