Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

this method call in constructor

 
siva chaitanya
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • 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 ?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • 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.
 
Rajdeep Biswas
Ranch Hand
Posts: 221
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • 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: 221
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • 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....?
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 4567
8
  • Mark post as helpful
  • send pies
  • 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 Java Windows XP
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thank you all for your kind reply, yes compiler places the super method call in parameterized constructor
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic