• 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

this() call in a constructor

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hello my lovelies!

I've got another (though one) for you. I have this piece of code (still testing):



So basally I am creating an anonymous object with an 'x ' parameter. That should invoke House(String name){...} constructor. Now that one invokes this() which if I get it right would trigger no-argument constructor of House class. So... according to my calculations I should get output: h hn x but I get b h hn x
Now is it because Building no argument constructor prints b and as House extends Building , Building's no argument constructor is going to be called as well? Or is there some higher Maths to it?
 
Ranch Hand
Posts: 76
3
IntelliJ IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You forgot to take into account that if no call to either this(...) or super(...) is issued, the compiler will automatically add in a no-arg super() call.

Does this answer your question?
 
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kamila Bertran wrote:House extends Building , Building's no argument constructor is going to be called as well?

Yes, have look at this
outputChain.png
[Thumbnail for outputChain.png]
 
Ganish Patil
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you think what would be the output, If we remove this() on line no 11? here
 
Kamila Bertran
Ranch Hand
Posts: 47
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, thanks guys. It does answer my question and clear my doubts. Ta!
 
Kamila Bertran
Ranch Hand
Posts: 47
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ganish Patil wrote:What do you think what would be the output, If we remove this() on line no 11? here



Hmm... Following the logic the output should be b hn x
 
Ganish Patil
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kamila Bertran wrote:Hmm... Following the logic the output should be b hn x

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic