Matt Gerrans
Posts: 1153
Nickname: matt
Registered: Feb, 2002
|
|
Re: toString() Proper use Please help!!!
|
Posted: Apr 17, 2002 2:52 PM
|
|
Let's stop making foo()s of ourselves and get back to basics by keeping things simple. toString() doesn't need to do anything fancy; the only requirement is that it returns a String. In your case, I would just do something simple like this:
public String toString()
{
return "Attendee name: " + attendee +
", ID: " + idNumber +
", Address: " + address;
}
Depdending on your needs, you could separate the items by newlines (instead of commas), omit the labels ("Attendee name: ", etc.), omit any information that is not relevant (maybe you don't want the address included) or include additional information (for instance, perhaps you want to append super.toString()).
Also, if you wanted different kind of String representations of your class, you can write other methods, like toTabSeparatedString(), or whatever. toString() is just the default method that will be called when you do something like System.out.println( yourObjectInstance ); any other operation that wants to cast your object to a Sting.
|
|