This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes this method call in constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "this method call in constructor" Watch "this method call in constructor" New topic
Author

this method call in constructor

siva chaitanya
Ranch Hand

Joined: Jul 05, 2011
Posts: 59
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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Jul 05, 2011
Posts: 59
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

Joined: Apr 06, 2010
Posts: 4369
    
    8

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

Joined: Mar 26, 2012
Posts: 186

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!!


The biggest gamble will be to ask a question whose answer you know in that it will challenge your theory | www.TechAspire.blogspot.in
Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 186


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

Joined: Mar 17, 2011
Posts: 7684
    
  19

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

Joined: Apr 06, 2010
Posts: 4369
    
    8

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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Jul 05, 2011
Posts: 59
Thank you all for your kind reply, yes compiler places the super method call in parameterized constructor
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: this method call in constructor