Home arrow static arrow Java Programming [Archive] - help with a toString method
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - help with a toString method
This topic has 22 replies on 2 pages.    1 | 2 | Next »

Posts:23
Registered: 8/4/03
help with a toString method  
Aug 3, 2004 8:51 AM



 
I have this ListIterator:
public void displayMyHardware()
{
Iterator components = myHardware.iterator();
while (components.hasNext())
{
System.out.println(components.next());
}

}

but when it shows the contents of my list, they are in the format Component$CPU@26e431.
What should my toString method look like, in order to make it show the real contents of the object?

I have a class called Component, and in this i have some nested classes, e.g. CPU with a few attributes, such as Maker, Model, Speed, Price.
To the same list I also add other types of object, e.g. Ram with the attributes Maker, Model, Size, Price.

I think I've heard that with the iterator and toString methods i could have it to show only what i need.

thanks
Romaniuz
 

Posts:316
Registered: 4/7/04
Re: help with a toString method  
Aug 3, 2004 9:02 AM (reply 1 of 22)



 
Override the toString method of your classes.
public class Foo{   String name;    public Foo(String name) {      this.name = name;   }    public @Override String toString() {      return name;   }}
 

Posts:19,725
Registered: 9/26/01
Re: help with a toString method  
Aug 3, 2004 9:04 AM (reply 2 of 22)



 
A nice toString() implementation to use in your classes is found in the "commons-lang" jar:
http://jakarta.apache.org/commons/lang/
 

Posts:27,518
Registered: 11/3/97
Re: help with a toString method  
Aug 3, 2004 9:10 AM (reply 3 of 22)



 
I would not suggest using toString() for functional purposes in an application.

Thus one should not use it to display something in a GUI, or to formal XML, etc.

It should be used to provide a representation that is convienent for debugging and tracing.
 

Posts:23
Registered: 8/4/03
Re: help with a toString method  
Aug 3, 2004 9:25 AM (reply 4 of 22)



 
if I do as you suggest, the result is that when I run the program, it just writes null instead of Component$CPU@26e431.

I added the code to the CPU class, so now it looks like:
public class CPU
{
String name;
public CPU(String f, String m, String z, int p)
{
fabrikant = f;
model = m;
frekvens = z;
pris = p;
this.name = name;
}

public String toString()
{
return name;
}
 

Posts:19,725
Registered: 9/26/01
Re: help with a toString method  
Aug 3, 2004 9:35 AM (reply 5 of 22)



 
if I do as you suggest, the result is that when I run
the program, it just writes null instead of
Component$CPU@26e431.

I added the code to the CPU class, so now it looks
like:
public class CPU
{
String name;
public CPU(String f, String m, String z, int p)
{
fabrikant = f;
model = m;
frekvens = z;
pris = p;
this.name = name;
}

public String toString()
{
return name;
}

You took an example, and turned it into buggy code. Way to go!
Now, would you care to turn the example into something of relevance for your class? Do you think adding a "name" property (and never setting it to anything, hence the null) would magically make it do what you want?
 

Posts:19,725
Registered: 9/26/01
Re: help with a toString method  
Aug 3, 2004 9:44 AM (reply 6 of 22)



 
(sigh) here's basically what you want:
public String toString() {
return getClass().getName() + "[fabrikant=" + fabrikant + ", model=" + model + ", frekvens=" + frekvens + ", pris=" + pris + "]";
}

Get rid of the bogus "name" property you added.

Equivalently, if you'd bother to download the jakarta commons-lang.jar like I pointed you to, and read some docs, you could simply use the org.apache.commons.lang.builder.ToStringBuilder class:
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
 

Posts:23
Registered: 8/4/03
Re: help with a toString method  
Aug 3, 2004 9:46 AM (reply 7 of 22)



 
Thank you for making me see what i did wrong at first, now it works
 

Posts:19,725
Registered: 9/26/01
Re: help with a toString method  
Aug 3, 2004 9:49 AM (reply 8 of 22)



 
Thank you for making me see what i did wrong at first,
now it works

You're welcome. It sure would have been nice if Sun had made the default Object.toString() do it the way ToStringBuilder does it, in the first place.
 

Posts:6,750
Registered: 1/25/04
Re: help with a toString method  
Aug 3, 2004 10:10 AM (reply 9 of 22)



 
They probably wanted to keep reflection out of the java.lang package, although they might be using it under the covers elsewhere and I don't know. They could also have used native code.
 

Posts:7,258
Registered: 1/31/02
Re: help with a toString method  
Aug 3, 2004 10:12 AM (reply 10 of 22)



 
I would not suggest using toString() for functional
purposes in an application.

Thus one should not use it to display something in a
GUI, or to formal XML, etc.

It should be used to provide a representation that is
convienent for debugging and tracing.

This is a good suggestion, but I know of at least one place where you have to abandon this good advice. If you use your own object as a TreeNode (passing it in as a parameter to the constructor of DefaultMutableTreeNode) then you need to override toString to have it return what you want to display in your UI. Would this be bad design?
 

Posts:19,725
Registered: 9/26/01
Re: help with a toString method  
Aug 3, 2004 10:13 AM (reply 11 of 22)



 
They probably wanted to keep reflection out of the
java.lang package, although they might be using it
under the covers elsewhere and I don't know. They
could also have used native code.

But in any case, how in the world does the default toString() implementation ever cut the mustard? It's never useful by default.
className@memoryAddress - yeah, that's real useful.
 

Posts:6,750
Registered: 1/25/04
Re: help with a toString method  
Aug 3, 2004 10:21 AM (reply 12 of 22)



 
It could surprise you with the runtime type of the object. That's the only use I can think of (I say surprise because if you know that's what you want to know you would do getClass()).
 

Posts:19,725
Registered: 9/26/01
Re: help with a toString method  
Aug 3, 2004 10:23 AM (reply 13 of 22)



 
It could surprise you with the runtime type of the
object. That's the only use I can think of (I say
surprise because if you know that's what you want to
know you would do getClass()).

I don't understand what the 'surprise' is about. The ToStringBuilder.reflectionToString() method includes the class name as well as its 'properties'. Basically the method does what the default Object.toString() does, plus add the properties of the object.
 

Posts:6,750
Registered: 1/25/04
Re: help with a toString method  
Aug 3, 2004 10:30 AM (reply 14 of 22)



 
I don't understand what the 'surprise' is about.

For example, you're debugging in your IDE and hover over an object. It gives you the toString result of it, and you say "oh, I didn't know it was one of THOSE, no wonder it's not working." Not a common occurrance.
 
This topic has 22 replies on 2 pages.    1 | 2 | Next »