In Order to get a consistent string representation of the classes used in a project, we can
- use a toString generator, e.g eclipse ‘Generate toString()…’
- use a builder mechanism in the
toString()
implementation.
From ToStringBuilder javadoc
- allowing field names
- handling all types consistently
- handling nulls consistently
- outputting arrays and multi-dimensional arrays
- enabling the detail level to be controlled for Objects and Collections
- handling class hierarchies
new ToStringBuilder(this)
append(...)
methodstoString()
class Person {
String name ="Joe";
int age = 22;
boolean smoker= true;
@Override
public String toString() {
return new ToStringBuilder(this)
.append("name", name)
.append("age", age)
.append("smoker", smoker).toString();
}
}
outputs
Person@d2cc05a[name=Joe,age=22,smoker=true]
The ToStringStyle
class controls String formatting for ToStringBuilder
. There are some predefined styles as constants in the ToStringStyle
class:
By subclassing the ToStringStyle
class one can define custom styles.