4

I see that Most of the times in the DTO object , the toString Method is actaully overridden .

For example :

public class Person implements Serializable {

    private String firstName;
    private String lastName;
    private int age;

    /**
     * Creates a new instance of Person
     */
    public Person() {
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    //Overriding toString to be able to print out the object in a readable way
    //when it is later read from the file.
    public String toString() {

        StringBuffer buffer = new StringBuffer();
        buffer.append(firstName);
        buffer.append("\n");
        buffer.append(lastName);
        buffer.append("\n");
        buffer.append(age);
        buffer.append("\n");

        return buffer.toString();
    }


}

Could anybody please tell me what is the use of doing so ??

Pawan
  • 31,545
  • 102
  • 256
  • 434

5 Answers5

10

The answer is in your code comment. When debugging, you want to be able to print a human readable representation of your object. If you don't override toString you will most likely have a representation like:

Person@129cfbb
Giann
  • 3,142
  • 3
  • 23
  • 33
8

It makes the debugger easier to use. In Eclipse (and I believe in nearly every IDE), the debugger shows the output of toString() on an object by default.

Edit: As other's have pointed out, there are plenty of other uses: logging, display in a GUI element, or anywhere else you need to convert the object to text for display.

Ryan Gross
  • 6,423
  • 2
  • 32
  • 44
  • 2
    true but its more than just for debugger. It's useful anywhere that you need a meaningful output of the DTO state – Steve Jun 24 '11 at 13:38
3

This is to have some proper output (as returned by toString() implementation) instead of the standard implementation of java.lang.Object (will be something like your.package.Person@...) when printing the object or when you append it to another string.

wjans
  • 10,009
  • 5
  • 32
  • 43
2

There are numerous uses. Among others : to display this object in a combobox in a GUI, to debug, for some unit testing assertions...

Raveline
  • 2,660
  • 1
  • 24
  • 28
0

Overriding toString to be able to print out the object in a readable way when it is later read from the file.

Sorry, couldn't resist. In all seriousness, toStrings are valuable to be overridden for any Java class. However, I would wager a DTO in particular is good to have a toString, because the object is meant to transfer data. Often, this means printing out the data into readable format.

Dustin Wilhelmi
  • 1,769
  • 2
  • 13
  • 27