Easy Java Bean toString() using BeanUtils

07/10/2008

I often want to have a String description of my beans for debugging or logging purposes, but hate having to manually concatenate the fields in my class to create a toString() method. This code snippet using Apache Commons is very helpful for just such occasions. Thanks to Paul Wilton who suggested this updated code snippet in the comments below.

public String toString() {
  return ReflectionToStringBuilder.toString(this, ToStringStyle.MULTI_LINE_STYLE);
}
Be Sociable, Share!