Member-only story

Effective Java! Override toString

Kyle Carter
2 min readJan 10, 2021

Today’s topic is a lot less prescriptive than our previous couple chapters where there were very mathematical sounding principles behind the contracts we were to follow. Today’s is much simpler. The toString() method. As Effective Java points out, Object gives us a default implementation of this method but it's rather not useful, especially when you are trying to debug and are simply greeted with something like Animal@23a5b2. The documentation for toString() even says that "It is recommended that all subclasses override this method." Well all classes are subclasses of Object so that must mean it is recommended that all classes should override this method.

One of the main uses of the toString method in my experience is to assist in debugging. Whether you override the toString method or not developers will try using the toString method for debugging. There is an art to knowing how much of the objects data to expose via the toString method. You should try to expose as much information as possible via the method but obviously a large object may not be able to practically expose all that information. In these cases we must just use our judgement to determine which fields are the most useful.

A decision that must be made is whether to document the format with which the method will return. With such a contract the users of your class can expect what will be returned and can use it in expected ways. If you do define the contract it can be a good idea to also provide a static factory method that takes the string representation and…

--

--

Kyle Carter
Kyle Carter

Written by Kyle Carter

I'm a software architect that has a passion for software design and sharing with those around me.

No responses yet