Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why private constructor?

 
Louie van Bommel
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1589
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12147
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tom Tolman
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic