public final class ToStringHelper extends Object
public class CustomClass
{
private String fieldOne = "HELLO";
private Object fieldTwo = null;
private boolean fieldThree = false;
private List fieldFour = new ArrayList(Arrays.asList("one","two","three"));
public String toString()
{
return
new ToStringHelper(this).
append("fieldOne", fieldOne).
append("fieldTwo", fieldTwo).
append("fieldThree", fieldThree).
append("fieldFour", fieldFour).
toString();
}
}
Return value of toString() in above example will be
"CustomClass@19621457 {fieldOne: HELLO, fieldThree: false, fieldFour: [one, two, three]}
"Constructor and Description |
---|
ToStringHelper() |
ToStringHelper(Object object)
Constructor that takes in the object whose information is to be added to the string
|
public ToStringHelper()
public ToStringHelper(Object object)
object
- The object whose classname and hashcode is to be included in the stringpublic ToStringHelper append(String fieldName, Object value)
fieldName
- The name of the field to appendvalue
- The value object of the field to appendCopyright © 2001-2017 The Apache Software Foundation. All Rights Reserved.