{"id":150,"date":"2015-09-03T09:37:35","date_gmt":"2015-09-03T16:37:35","guid":{"rendered":"http:\/\/willclausen.com\/?p=150"},"modified":"2015-09-03T09:37:49","modified_gmt":"2015-09-03T16:37:49","slug":"effective-java-item-10","status":"publish","type":"post","link":"http:\/\/willclausen.com\/?p=150","title":{"rendered":"Effective Java: Item #10"},"content":{"rendered":"<p><strong>Item #10: Always override <code>toString<\/code><\/strong><\/p>\n<p>All classes that extend the Object class, which is to say all classes, have the <code>toString<\/code> method. When <code>toString<\/code> is not overriden, this method simply prints the name of the class, followed by an &#8220;@&#8221;, followed by the id of the instance of the class. This is not super helpful to people using your class and trying to debug.<\/p>\n<p>So, as a means of providing useful information about the class in an easily accessible way for people using your class, you must always override <code>toString<\/code>.<\/p>\n<p>There are some important considerations that go into overriding <code>toString<\/code>. First, the method should return all of the interesting information in the object. In the event that there are a huge number of member variables, <code>toString<\/code> should print a summary of the object.<\/p>\n<p>Another important consideration is whether to specify the format of the particular <code>toString<\/code> implementation you decide to use. Keep in mind that if you do, you are stuck with that format forever, as changing it would break code that uses your class and that method. Whatever you decide when it comes to specifying a format, you must document it, so that users of the class can have a clear understanding of what they can expect.<\/p>\n<p>Finally, you should make sure to provide access to all of the important information returned by <code>toString<\/code>. Otherwise, users of your class have to parse the toString, reducing performance and increasing hassle.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Item #10: Always override toString All classes that extend the Object class, which is to say all classes, have the toString method. When toString is not overriden, this method simply prints the name of the class, followed by an &#8220;@&#8221;, followed by the id of the instance of the class. This is not super helpful [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45],"tags":[51,50],"class_list":["post-150","post","type-post","status-publish","format-standard","hentry","category-effective-java","tag-override","tag-tostring"],"_links":{"self":[{"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/posts\/150","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/willclausen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=150"}],"version-history":[{"count":1,"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/posts\/150\/revisions"}],"predecessor-version":[{"id":151,"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/posts\/150\/revisions\/151"}],"wp:attachment":[{"href":"http:\/\/willclausen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=150"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/willclausen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=150"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/willclausen.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=150"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}