Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes 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 "private constructors" Watch "private constructors" New topic
Author

private constructors

Rajani Sudhakar
Ranch Hand

Joined: Apr 28, 2004
Posts: 60
class Vict{
private Vict(){
System.out.print("Vict");
}
}

public class BLBeck extends Vict{
public static void main(String argv[]){
new BLBeck();
}

BLBeck(){
System.out.print("BLBeck");
}
}


Compiling and running the above code gives a complier error...
why is it so?

I guess private constructors are allowed..
If so... why does the compipler complain?

Thanks in advance,
rajani.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Private means private no matter what it modifies. Private means that you can't access that member from outside the class. That includes constructors.

Private constructors are useful when you want to control the instantiation of a class. That is the case of the Singleton pattern:



Private constructors are not used often, but they can be quite useful.


SCJP Tipline, etc.
K Ville
Ranch Hand

Joined: Aug 16, 2003
Posts: 58
In addition, marking a variable/method "private" makes it implicitly final, too


Thinking is the talking of the soul with itself...-Plato
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by K Ville:
In addition, marking a variable/method "private" makes it implicitly final, too


No it doesn't!

Private and final are two very different things. If you declare a member variable to be final, it means that its value can not change once set - it becomes a constant. A private member variable has no such requirement - it can change constantly.

A final member method can not be overridden by a subclass but it is still inherited by that subclass. A private member method can not be overridden because it is not inherited by the subclass. It is, however, perfectly legal for the subclass to define a method with the same signature as the parent's private method because, in essence, it is a brand new method.

Make sure you keep them straight - private does not mean final and vice versa.
Joe Skora
Greenhorn

Joined: Jan 29, 2004
Posts: 7
Every constructor must have a call to this() or super(), if you don't include it, the compiler will add a call the no-arg constructor of the parent. So, the constructor in BLBeck becomes:

BLBeck(){
super();
System.out.print("BLBeck");
}

But, in this case the Vict's no-arg constructor is private so you get the compile error. Vict needs some constructor that can be reached by a subclass or it can't be extended.

If Vict had a different constructor (i.e. different argument signature) that was protected (or default if BLBeck is in the same package), you could explicitly call that in your subclass and the compiler would not need to generate the super() call. Then it would compile.

Joe Skora
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Joe Skora:
Vict needs some constructor that can be reached by a subclass or it can't be extended.


That's not entirely true. You are still allowed to extend the class, you just can't make an instance of your subclass. There are, however, many operations that a class can perform without an instance - just take a look at java.lang.Math.
Raj Poosarla
Greenhorn

Joined: May 22, 2004
Posts: 8
A Private contructor means that an instance of the class can be created only from within the class, not from outside the class.

In your case, as you are trying to create it from another class, the compiler complains.
Rajani Sudhakar
Ranch Hand

Joined: Apr 28, 2004
Posts: 60
Thankyou soo much for ur replies..

regards,
rajani.
 
Don't get me started about those stupid light bulbs.
 
subject: private constructors
 
Similar Threads
Access specifiers for constructors
Flow control!
constructor doubt
Difference between private constructor and making class final ?
Doubt regarding private contructors