When is it desired to not implement toString() in Java?

后端 未结 24 2104
心在旅途
心在旅途 2020-12-10 00:45

A lead developer on my project has taken to referring to the project\'s toString() implementations as \"pure cruft\" and is looking to remove them from the code base.

<
24条回答
  •  死守一世寂寞
    2020-12-10 01:07

    It makes good sense because you always have problems with toStrings showing too little or too much information.

    It may make sense to your team to use the ToStringBuilder in Jakarta Commons Lang instead:

    System.out.println("An object: " + ToStringBuilder.reflectionToString(anObject));
    

    which introspects the object, and prints out the public fields.

    http://commons.apache.org/lang/api-2.3/org/apache/commons/lang/builder/ToStringBuilder.html

提交回复
热议问题