3

I am working on a Spring project wherein I have a POJO class

    public class Owner extends Person {

        private String address;

        private String city;

        private String telephone;

            Getters and Setters 

@Override
    public String toString() {
        return new ToStringCreator(this)

        .append("id", this.getId())

        .append("new", this.isNew())

        .append("lastName", this.getLastName())

        .append("firstName", this.getFirstName())

        .append("address", this.address)

        .append("city", this.city)

        .append("telephone", this.telephone)

        .toString();
    }
}
  1. I have two doubts why a toString() method is being used here? What is its use?
  2. What is ToStringCreator method doing?
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
underdog
  • 4,447
  • 9
  • 44
  • 89
  • 2
    For your first question you can refer this post: http://stackoverflow.com/questions/3615721/how-to-use-the-tostring-method-in-java – Chaitanya Jan 04 '14 at 06:09
  • 3
    For your second question,the ToStringCreator class uses `StringBuilder` internally and formats the output based on the default `ToStringStyler` class which is `DefaultValueStyler` of `Spring` where it gives the output in the form of `[classname@hashcode 'key1'='value1','key2='value2']` – Chaitanya Jan 04 '14 at 06:21

1 Answers1

1

As per official docs

ToStringCreator is a Utility class that builds pretty-printing toString() methods with pluggable styling conventions. By default, ToStringCreator adheres to Spring's toString() styling conventions.

It is a helper class for customizing toString() presentation.

Sai Surya Kattamuri
  • 1,046
  • 12
  • 22