Help coderanch get a
new server
by contributing to the fundraiser
  • 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 ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

RHE P182

Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if an explicit call to another constructor is made using this(..),then the superclass constructor is not called until the other constructor runs
I write a program to test it:
1.class Super {
2. Super(){System.out.println("Super");}
4.class Based extends Super {
5. Based() {this(20);System.out.println("Based");}
6. Based(int i) {
7. System.out.println(i);
8. }

9. public static void main(String []fred) {
10. Based ba1= new Based();
11. }
I expect the result is From the above quoted)
But the actual result is:

My question is why generate such result? and what does RHE mean by saying that?
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My friend, the other constructor in this case is called first, but remember! All constructors call the superclass constructor first. So in this case, the constructor with an int parameter is calling the super constructor before running itself. Try calling another type of super constructor from the constructor that takes in int as a parameter and you will end up running that constructor first.
[This message has been edited by Khalid Bou-Rabee (edited July 17, 2000).]
"Don't believe every tiny ad you see on the internet. But this one is rock solid." - George Washington
We need your help - Coderanch server fundraiser
    Bookmark Topic Watch Topic
  • New Topic