• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

this method call in constructor

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a class called B

and there is class A which extends from B


My question is this(); method which is used to call other constructor from one constructor but compiler implicitly places super(); method call in constructor to call super class constructor while compiling but there cannot be this() and super() method calls in same constructor ?
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
either this or super is allowed as first statement in a constructor block ; *if you dont specify* super or this,
then compiler insert super() call as a first statement to every constrcutors(which dont have super/this call explicitly) in a class.
 
siva chaitanya
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you swamy for your reply but what i am asking is i have this(10); in that constructor which i placed to call other constructor but compiler places super method in the same constructor which i have this method, but both statements are not allowed in the constructor
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, the compiler does not place the super() call in the A() constructor. But it does place it in the A(int) constructor, and you're making the A() constructor call that.

A superclass constructor will always be called eventually.
 
Ranch Hand
Posts: 231
1
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your int-arg A(int a) constructor, compiler implicitly places a super(); for which the control goes to super class B's no-arg constructor, executes (prints on console "B constructor") and comes back.
Now its your job to see if you have placed any other this or super call in the int-arg constructor, and you have your answer!!
 
Rajdeep Biswas
Ranch Hand
Posts: 231
1
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A superclass constructor will always be called eventually.



Is it not due to the fact that at least one of the constructors of a class will have super(); call, either implicit or explicitly, else there will be recursive constructor invocation....?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rajdeep Biswas wrote:Is it not due to the fact that at least one of the constructors of a class will have super(); call, either implicit or explicitly, else there will be recursive constructor invocation....?


No. What Matthew said is correct. A class cannot be constructed unless ALL superclass constructors are called, which is why Java adds a super() call if you don't include one yourself.

Winston
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Rajdeep Biswas wrote:Is it not due to the fact that at least one of the constructors of a class will have super(); call, either implicit or explicitly, else there will be recursive constructor invocation....?


No. What Matthew said is correct. A class cannot be constructed unless ALL superclass constructors are called, which is why Java adds a super() call if you don't include one yourself.


I think that's actually what Rajdeep meant. If you tried to avoid any explicit or implicit super() calls by adding a this(...) call to every single constructor you'd get a recursive loop.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote: If you tried to avoid any explicit or implicit super() calls by adding a this(...) call to every single constructor you'd get a recursive loop.


Yes. interestingly, unlike method recursive call between methods, constructor recursive call produce compiler error!
 
siva chaitanya
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for your kind reply, yes compiler places the super method call in parameterized constructor
 
I found a beautiful pie. And a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic