• Post Reply Bookmark Topic Watch Topic
  • New Topic

Super and this  RSS feed

 
Matt Road
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Just one basic questions :
I have just read on the book OCA 8 that SUper and This call to constructors must both be called in the first statement.
So, I was wondering how I can use both of the two calls in the same class since they need to be called as first statemt.

Thank you for your time!

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matt Road wrote:
So, I was wondering how I can use both of the two calls in the same class since they need to be called as first statemt.


You can't. It is either one or the other within a constructor.

Furthermore, it doesn't make sense to use both -- as the "this" call to another constructor will have a "super" call in the other constructor. This will make the constructor call indirectly the "super" constructor twice.

Henry
 
Matt Road
Greenhorn
Posts: 22
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SO when I'll use a THIS call to a constructor , Java won't add any hided default SUPER() call. Am I right ?

Thank you
 
Liutauras Vilda
Sheriff
Posts: 4914
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matt Road wrote:SO when I'll use a THIS call to a constructor , Java won't add any hided default SUPER() call. Am I right ?

Thank you
Hi Matt. No.
Read once again what Henry wrote earlier in his 2nd 3rd sentence.
One important thing: it is "this" and "super", not "THIS" or "SUPER".
 
Campbell Ritchie
Marshal
Posts: 56522
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not THIS please. It is this.
It is all in the Java® Language Specification, though that can be difficult to read.

If you neither write this(...); nor super(...); the compiler will implicitly add
super();
with empty ().
Forget about what it says about type arguments in the JLS.
That JLS Section wrote:If a constructor body does not begin with an explicit constructor invocation and the constructor being declared is not part of the primordial class Object, then the constructor body implicitly begins with a superclass constructor invocation "super();"
 
Campbell Ritchie
Marshal
Posts: 56522
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not a new feature: has been in all versions of Java® which I can remember. Moving discussion. Hope you don't mind, Vijitha and Rob.
 
Matt Road
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So now I don't understand pag 254 of the OCA 8 book by Jeanne Boyarsky, Scott Selikoff


In the class Kangaroo there are no constructors su Java should add a super() call but the code should not compile since in the parent class Marsupial there are no constructor with empty (). Where I wrong ?

Thank you
 
Liutauras Vilda
Sheriff
Posts: 4914
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matt Road wrote:In the class Kangaroo there are no constructors su Java should add a super() call but the code should not compile since in the parent class Marsupial there are no constructor with empty (). Where I wrong ?
I'm not sure I correctly understand your question. But let's try to clarify this point:
So, class B fails to compile, because implicitly added "super" call to a base class constructor cannot find defined no-argument constructor. So in such a case you have to add super call explicitly with the right parameters list, for instance "super(4)".
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matt Road wrote:So now I don't understand pag 254 of the OCA 8 book by Jeanne Boyarsky, Scott Selikoff

I'm afraid quoting us a page from a book we don't have in front of us is all but useless.

What exactly does it say (copy and paste it if you need to), and what exactly about THAT is it that you don't understand?

However, from what you go on to say (although it's very sloppily worded), I suspect you're pretty close the mark.

Winston
 
Matt Road
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh I got it. super and this can be used only inside a constructor, right ? I can't use inside a method for calling a constructor, right ?
 
Campbell Ritchie
Marshal
Posts: 56522
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not quite. You can only use super(...); and this(...); in the first line of a constructor.
You can use super. and this. or this elsewhere but not in static methods.
You can only call a constructor elsewhere after the new operator.
 
Matt Road
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gotcha ! Thank you!
 
Liutauras Vilda
Sheriff
Posts: 4914
334
BSD
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You can only use super(...); and this(...); in the first line of a constructor.
Just to be more precise. Not necessarily in the first line of constructor, but must be as a first statement in constructor. First line could be a comment for example.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!