• 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 supper() and this(..)

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why does the compiler give me error on 'line 2', but if I delete line 1, there's no compiler error?
------------------------------------
public class Test extends A {
int y=1;
int a=6;
Test() {
super(); //line 1
this(6); //line 2
}
Test(int x) {
if ( x > y )
a = y*6;
else
a = y*9;
a++;
System.out.println(a);
}
public static void main(String [] args) {
Test t = new Test();
}
}
class A{
A(){
System.out.println("OK");
}
}
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because you can't call both super() and this() from a constructor. It is either one or the other
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
super() or this() calls must be the first statement in the constructor. This, of course, implies that you cannot have both calls in the same constructor.
 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Problem:
Constructor of superclass is called twice.
One time explicitly with super() in Test() and another time
implicitly with constructor Test(int x) which is called from
constructor Test() with this().
Thomas
 
Don't listen to Steve. Just read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic