• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Super and this

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!

 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 8524
615
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
Marshal
Posts: 76862
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76862
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 8524
615
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)".
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76862
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gotcha ! Thank you!
 
Liutauras Vilda
Sheriff
Posts: 8524
615
Mac OS X VI Editor BSD Linux
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic