Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java toString for any object [duplicate]

I have a lot of data objects, and I want to be able to generate a String representing each object, without implementing a toString method for each one.

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

any other ideas?

thanks.

like image 228
AAaa Avatar asked Jul 24 '11 15:07

AAaa


1 Answers

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:

@Override
public String toString() {
    return ToStringBuilder.reflectionToString(this);
}
like image 142
AlexR Avatar answered Oct 05 '22 03:10

AlexR