Background

If you create a new class and do not override the toString method then, by default, you will get Object.toString. This returns the following:

getClass().getName() + '@' + Integer.toHexString(hashCode())

The javadoc recommends that all subclasses should override the toString.

Tips

  1. Use the @Override annotation above the method signature to ensure it is correct.

  2. NEVER EVER print! Your toString method should return the String representation of your class.

  3. Always document what format you are using in your comments/javadoc.

  4. If your class provides a toString method, also provide Getters so that programmers using your class have a way to access the data used to create your toString method.
    The user should never have to parse your toString method to obtain information.

  5. Consider using String.format() to enhance the flexibility and readability of your code.