This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Is it possible for a class to have a private constuctor??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Is it possible for a class to have a private constuctor???" Watch "Is it possible for a class to have a private constuctor???" New topic
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: 818

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: 11153
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is it possible for a class to have a private constuctor???
 
Similar Threads
invoking java function with java script Onclick event
Is it possible to write SCJP 6.0 by using SCJP 5.0's coupon
bean:write query
Can one action class have more than one Form class
button