How to override toString() properly in Java?

The toString is supposed to return a String.

public String toString() { 
    return "Name: '" + this.name + "', Height: '" + this.height + "', Birthday: '" + this.bDay + "'";
} 

I suggest you make use of your IDE’s features to generate the toString method. Don’t hand-code it.

For instance, Eclipse can do so if you simply right-click on the source code and select Source > Generate toString

Leave a Comment