Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

super().this()!

 
Brian Smith
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi folks,
is the following way of using this() is legal? why or why not?


i know we can call constructor in A like this super("test"); but in class B, i have used this() with super() to call a constructor in A. tell me why it's invalid?
thanks.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Couple things:
1). Constructors do not have return values; therefore, you cannot chain them together.
2). Secondly, since A has a defined constructor, a default, no-argument constructor will not be provided; you will get a "cannot resolve symbol" error when you call super()
3). You do not have a constructor that takes a String as a parameter in your B class. The call to this(String) will not resolve ("cannot resolve symbol" error message.
Remember, Constructors are not Methods; they do not return anything, they are not inherited, and they do not participate in polymorphism.
I'm not sure what result you were trying to acheive with that syntax, but to get the result I think you want, try:

[ July 15, 2003: Message edited by: Joel McNary ]
 
Brian Smith
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joel McNary:
Couple things:
I'm not sure what result you were trying to acheive with that syntax
[ July 15, 2003: Message edited by: Joel McNary ]


class B extends A{
B(){
super().this("test");
}
}

what i am trying to do here is calling a constructor in superclass A. my question is can i use this()with super() the way i am using?
thanks.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you just trying to say

That's the right syntax to pass constructor arguments to a parent class.
[ July 15, 2003: Message edited by: Ernest Friedman-Hill ]
[ July 15, 2003: Message edited by: Ernest Friedman-Hill ]
 
Brian Smith
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No that's not what i am trying to say. what i am asking is that is super().this("test"); valid?

][/qb]<hr></blockquote>
[ July 15, 2003: Message edited by: Namaste Sathi ]
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer is no, that syntax is not valid. However, you can call the constructor in class A with the syntax provided by myself and Ernest.
[ July 15, 2003: Message edited by: Joel McNary ]
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not even close. What's the point anyway? If you're trying to call a constructor in your superclass just use super("astring") or this.super("astring").
 
Brian Smith
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys for your help. i appreciate your help Joel and Ken with super().this("somevalue");
thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic