The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
January 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Just to exercise your own control

Posted by Kishori Sharan on January 07, 2001 at 12:33 PM

How: It is used in same way as other type of constructors e.g. public, protected , default access, just to create an instance of the class. However, a private constructor can be used only inside that class methods to create objects of that class.
Why: I can give you two good examples for why.
1) To use Singleton pattern so that you have only one instance of you class all the time. In this case make your constructors private and give a method where in you maintain only one instance of your class.
2) If you don't want to let others inherit your class then you can declare your class final or alternatively you can declare all construtors private. In this case you will have to create a default construtor too and make it private even if you don't create one.

Thanx
Kishori




Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us