• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

private constructors

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In addition, marking a variable/method "private" makes it implicitly final, too
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou soo much for ur replies..

regards,
rajani.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic