aspose file tools*
The moose likes Java in General and the fly likes super().this()! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "super().this()!" Watch "super().this()!" New topic
Author

super().this()!

Brian Smith
Ranch Hand

Joined: Oct 26, 2002
Posts: 232
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

Joined: Aug 20, 2001
Posts: 1821

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 ]

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Brian Smith
Ranch Hand

Joined: Oct 26, 2002
Posts: 232
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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 ]

[Jess in Action][AskingGoodQuestions]
Brian Smith
Ranch Hand

Joined: Oct 26, 2002
Posts: 232
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

Joined: Aug 20, 2001
Posts: 1821

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

Joined: Jul 15, 2003
Posts: 1078
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

Joined: Oct 26, 2002
Posts: 232
thanks guys for your help. i appreciate your help Joel and Ken with super().this("somevalue");
thanks.
 
jQuery in Action, 2nd edition
 
subject: super().this()!