• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Keyword 'this' not preventing call to super constructor.

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All, I understood that if you put the keyword 'this' in a constructor the compiler doesn't insert super().
But this code still calls Alpha even though it has a 'this' in the Beta constructor:



The output is: Alpha constructor Beta constructor

Thanks in advance.
 
Ranch Hand
Posts: 151
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe what you're looking for is using this() to call another constructor. Simply using the this keyword doesn't magically do anything.

But you'll eventually still be forced to call a super constructor. Why would you want to avoid that?
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ste Davies wrote:Hi All, I understood that if you put the keyword 'this' in a constructor the compiler doesn't insert super().
But this code still calls Alpha even though it has a 'this' in the Beta constructor



Ste, Can you tell us the reference where you encountered this fact? Usually super has to be the first statement in the constructor (if explicitly mentioned). Compiler automatically inserts a call to super() [Default super constructor) and your Super class must provide a default constructor (if at all you have other overloaded constructors).

So the output is as expected.
 
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when we create a sub class object, it calls appropriate constructor of the sub class which in turn calls the default constructor of superclass (though we can change this behavior by using super()) Now the super class constructor will be executed first then the sub class's constructor.
So, the order of invocation is always opposite to the order of execution.


I think this is enough.
And talking about your "this", it is merely representing the current instance of Beta();.....whatever you understood is wrong..!! tc
 
Ste Davies
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys.

Kevin: So its this() which excludes super() not this.something ?

Mohammed: The reference was Sierra & Bates, chapter2, P160: "The compiler will add a call to super() unless you have already put in a call
to this() or super()."
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin's right. You can use this(...); to call another constructor in the same class, and if you do this replaces the call to a super-class constructor.

But eventually you're going to have a constructor that doesn't do this (or you're going to have an infinite loop, which is bad ). So eventually a super-class constructor will always be called.
 
Kevin Workman
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ste Davies wrote:Thanks guys.


No problem.

Ste Davies wrote:Kevin: So its this() which excludes super() not this.something ?


What happened when you tried it out?
 
Ste Davies
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I did this:



and got:

Alpha constructor
Beta string constructor
Beta constructor

Its still the same.
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ste Davies wrote:Well I did this:



and got:

Alpha constructor
Beta string constructor
Beta constructor

Its still the same.



So Beta() calls Beta(String). Beta(String) calls Super() and then after Super() is executed it comes back to Beta(String) and then back to Beta().

You can see in the output that Beta String Constructor is printed before Beta Constructor.
 
Kevin Workman
Ranch Hand
Posts: 151
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ste Davies wrote:
Alpha constructor
Beta string constructor
Beta constructor

Its still the same.



If you still don't understand what's going on, I'd suggest stepping through the program with a debugger so you can see what's happening line by line.
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kevin Workman wrote:
If you still don't understand what's going on, I'd suggest stepping through the program with a debugger so you can see what's happening line by line.



Agree. It will help you to figure out the flow.
 
Ste Davies
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great, ok I see now. Thanks all
 
swaraj gupta
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ste, i think you should reconsider my previous reply, to fix your concepts..........
 
Ste Davies
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok thanks Swaraj.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!