Is it good or bad practice auto-generating toString methods for some simple classes?
I was thinking of generating something like below where it takes th
Considering some old answers including @Steve's, I'd like to add answer as per latest library.
Add dependency
org.apache.commons
commons-lang3
3.10
In your class
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
public class User {
...
@Override
public String toString() {
return ReflectionToStringBuilder.toString(this);
}
}
You can exclude certain fields as below
...
@Override
public String toString() {
return ReflectionToStringBuilder.toStringExclude(this, "name"); // Name will be excluded from toString output
}