• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

About private constructors!

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you Fei . Futhermore , how can i control the numbers of object more than one ?
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Valentin. Now I understand. As you can see I am a greenhorn.
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The fact is I am a greenhorn. Your comments and advice are appreciated. I am here to learn.
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic