Auto-generating toString Method

Posted by Gordon on Stack Overflow See other posts from Stack Overflow or by Gordon
Published on 2010-04-16T13:36:02Z Indexed on 2010/04/16 13:43 UTC
Read the original article Hit count: 322

Is it good or bad practice auto-generating toString methods for some simple classes?

I was thinking of generating something like bellow where it takes the variable names and produces a toString method that prints the name followed by it's value.

private String name;
private int age;
private double height;

public String toString(){
    Formatter formatter = new Formatter();
    return formatter.format("Name: %s, Age: %d, Height %f", name, age, height).toString();
}

© Stack Overflow or respective owner

Related posts about automation

Related posts about best-practices