Re: this.toString
From: Miguel De Anda (_sodamnmad__at__hotmail_._com_)
Date: 10/18/03
- Next message: Miguel De Anda: "Re: clueless student trying to parse XML"
- Previous message: Chris Smith: "Re: Modification of a SWING tree node label"
- In reply to: Shaun: "this.toString"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 17 Oct 2003 22:16:21 GMT
"Shaun" <shaun@mania.plus.com> wrote in message
news:UcYjb.11806$kA.2975528@wards.force9.net...
> Hi,
>
> I am a java beginner. Could someone please explain why calling the this
> keyword makes an implicit call to Objects toString method as in the
> following code example taken from Java How To Program?
>
<snip>
> // return String representation of Point4 object
> public String toString()
> {
> return "[" + getX() + ", " + getY() + "]";
> }
>
Since you overwrote the toString(), it shouldn't make the call to the
Object's toString(). It should end at the Point's toString(). If you didn't
have the toString() method there, it would return the string that the Object
class returns, usually funky stuff. I think the question you are really
asking is why the toString() method gets called on the ("..." + this) part.
The simplest reason I can think of is because you are implicitly trying to
convert the Point object to a String object. To do this, it uses the
toString() method because that's just the way they chose to do it. They
could have picked public String converThisObjectToAString() but I guess they
figured it was too long.
- Next message: Miguel De Anda: "Re: clueless student trying to parse XML"
- Previous message: Chris Smith: "Re: Modification of a SWING tree node label"
- In reply to: Shaun: "this.toString"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|