| Author |
Is it possible for a class to have a private constuctor???
|
Abhi vijay
Ranch Hand
Joined: Sep 16, 2008
Posts: 509
|
|
|
How is that possible?/
|
 |
M Srilatha
Ranch Hand
Joined: Aug 27, 2008
Posts: 137
|
|
Constructors can be declared as private. A private constructor means only code within the class itself can instantiate an object of that type, so if the private constructor class wants to allow an instance of the class to be used, the class must provide a static method or variable that allows access to an instance created from within the class.
|
Thanks,<br />Srilatha M
|
 |
sudipto shekhar
Ranch Hand
Joined: Apr 02, 2008
Posts: 813
|
|
Originally posted by M SRILATHA: Constructors can be declared as private. .....so if the private constructor class wants to allow an instance of the class to be used, the class must provide a static method or variable that allows access to an instance created from within the class.
I did not get this.Can you be a little clear,please?
|
Regards, Sud.
SCJP 5 ScjpFAQ JLS
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9952
|
|
The classic example stems from the singleton design pattern. You can find tons of literature on that. but... if you have a static method, that method can be called even if no instance of the class has been created (think of all the Math class functions). Now, those static functions are part of the class, so those methods CAN call private functions of the class. So, if the constructors are private, you can call the (public) static methods, which can then call the constructors. the static method then returns the reference to the object.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Santhi Bharath
Ranch Hand
Joined: Jun 03, 2008
Posts: 75
|
|
up to my knowledge private constructors are used generally in the following cases 1. to call from the public constructors for your customized needs and when you don't want to disclose to out side world 2. to create an object of your own class and when you don't want other classes to create objects of your own class directly like in the sigleton pattern see the below example from source code of java.lang.Runtime class public class Runtime { private static Runtime currentRuntime = new Runtime(); public static Runtime getRuntime() { return currentRuntime; } private Runtime() {} that's why we can't instantiate Runtime directly means using new operator
|
thanks and regards<br />Santhi Bharath<br />SCJP 5.0, SCWCD 5.0
|
 |
Santhi Bharath
Ranch Hand
Joined: Jun 03, 2008
Posts: 75
|
|
up to my knowledge private constructors are used generally in the following cases 1. to call from the public constructors for your customized needs and when you don't want to disclose to out side world 2. to create an object of your own class and when you don't want other classes to create objects of your own class directly like in the sigleton pattern see the below example from source code of java.lang.Runtime class public class Runtime { private static Runtime currentRuntime = new Runtime(); public static Runtime getRuntime() { return currentRuntime; } private Runtime() {} that's why we can't instantiate Runtime directly means using new operator
|
 |
Anoobkumar Padmanabhan
Ranch Hand
Joined: Aug 08, 2007
Posts: 103
|
|
You are correct Santhi. sometimes, we can restrict that the objects of our class can built by the methods of the same class only(for getting the singleton property or for some other specific needs). In some cases, the public constructor will call the private one, for some manipulations.
|
Thanks<br /> <br />Anoobkumar<br />SCJP 1.5
|
 |
 |
|
|
subject: Is it possible for a class to have a private constuctor???
|
|
|