The Source for Java Technology Collaboration
User: Password:
Register | Login help    

Search

Online Books:
java.net on MarkMail:


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.

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

Sample output:  
[com.company.business.Address: 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, com.companyname.business.etc..)

Any thoughts?

Related Topics >> Programming      
Comments
Comments are listed in date ascending order (oldest first)