AAaa AAaa - 1 year ago 85
Java Question

java toString for any object

I have a lot of data objects, and I want to be able to generate a

representing each object, without implementing a
method for each one.

I'm thinking of reflection for getting the fields and its values.

any other ideas?


Answer Source

You are welcome to use ToStringBuilder from jakarta. It has 2 modes one requires adding all fields you need using API, other is reflection based:

public String toString() {
    return ToStringBuilder.reflectionToString(this);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download