Skip to main content

toString() - any format recommendation?

Posted by malcolmdavis on June 10, 2004 at 5:30 PM PDT

Many times the toString() format is dependent on how the developer wants to see the state. I've seen formats to accommodate Swing, console, the debugger, and log files. To help format Value/Transfer objects consistently, I use a utility method that employs reflections.

public String toString() {
    return ToString.format(this);

Sample output: 
[ Key=0, State=AL, Country=USA, City=Helena, Street=1313 MockingBird Lane, Zip=35000]

To mitigate issues with logging, I avoid carriage return and line feeds. To help differentiate embedded objects, the objects are enclosed with '[' and ']'.

[com....Customer: …, Address=[com....Address:…], Phone=[com....Phone:…],…,…] 
(where com... is short for the entire package path,

Any thoughts?

Related Topics >>