aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "About private constructors!" Watch "About private constructors!" New topic
Author

About private constructors!

Tony Sam
Ranch Hand

Joined: Nov 19, 2001
Posts: 32
hi,all
i am wondering that whether a class with private constructors is the same as a final class,if this is ,is the private constructors unnecessary?
Thanx in advance!


Tony Sam<BR>One want to be a SCJP...
Fei Ng
Ranch Hand

Joined: Aug 26, 2000
Posts: 1242
i dont think they are the same but thats my opinion.
Final, that means the class is Final. It is finish. No more function should add to it. Thus, shouldn't be extended by any other class.
private, means that you can control the number of Object of that class in your code. For example, you only want 1 object of that class becuase of your mult-Thread program. Since some other Thread can init the same Object again and again. Using the Private constructors helps. This is a pattern but i forgot the name for this Pattern.

[This message has been edited by FEI NG (edited December 02, 2001).]
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Fei gave a very good explanation.
Concerning the name of the design pattern, it is called Singleton. But a class with a private constructor is not only for creating only one instance, just a specific number of instances. Say maybe you just want 5 instances of your class to be created.
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Jim Hall
Ranch Hand

Joined: Nov 29, 2001
Posts: 162
I think essentially they are the same.
A final class cannot be subclassed.
A class with all private constructors cannot be subclassed either because there would be no way to call the super classes constructor.
Tony Sam
Ranch Hand

Joined: Nov 19, 2001
Posts: 32
thank you Fei . Futhermore , how can i control the numbers of object more than one ?
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Jim, both can achieve the same goal but they are not the same !
Tony, generally a Singleton pattern goes together with a Factory pattern which controls the number of instances you can create.
Usually, the class provides only private constructors and some static methods like getInstance() which return an instance of the class and if needed create some. For instance, the following code allows only one instance to be created:

If you want more instances, just control the number of instances you create by storing them in a List and checking the length of the list before creating a new instance. This way you can manage a pool of instances for your classes, much like the private String pool is managed...
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
[This message has been edited by Valentin Crettaz (edited December 03, 2001).]
Fei Ng
Ranch Hand

Joined: Aug 26, 2000
Posts: 1242
Originally posted by Jim Hall:
I think essentially they are the same.
A final class cannot be subclassed.
A class with all private constructors cannot be subclassed either because there would be no way to call the super classes constructor.

But don't use private constructors becuase you don't want people to subclass you class. It is really not clear!!! Constructors are for constructing Object thus foucing on creating Objects. Final means it is done! the BluePrint of the class is DONE, no more functions.
Tony, Patterns are great for OOA/D. If you want to know the detail i suggest you get a Patterns book after the your exam.
and good luck!!! Best wishes!
[This message has been edited by FEI NG (edited December 03, 2001).]
Jim Hall
Ranch Hand

Joined: Nov 29, 2001
Posts: 162
Thanks Valentin. Now I understand. As you can see I am a greenhorn.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Hi Jim,
I didn't intend to emphasize the fact that you were a greenhorn, I just wanted to make things clear I apologize if I made you think that way
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
Jim Hall
Ranch Hand

Joined: Nov 29, 2001
Posts: 162
The fact is I am a greenhorn. Your comments and advice are appreciated. I am here to learn.
Hussain
Ranch Hand

Joined: Nov 22, 2001
Posts: 110
Hey the worth of private constructors :
class A
{
private A(){}
}
class B extends A
{
}
the above will not compile as the Base class have A private constructors
but interretingly this will compile
abstract class A {}
class B extends A{}
(as super and this are special type of object they formed insdie)
u cant call super and this outside the class like this
class A
{
int i=0;
}
class B
{
public static void main(String [] args)
{
System.out.println(new A().this.i);
//or like this
System.out.println(A.this.i);
}
}
thankx

Muhammad Hussain<br />Sun Certified Java Programer (SCJP2)<br />Sun Certified Web Component Devloper SCWCD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About private constructors!