SE450: Java classes: toString [20/22] Previous pageContentsNext page

the toString method is invoked whenever you use the object in System.out.println. A good String representation of your object makes debugging it much easier than the standard String representation. A good rule of thumb is to include significant fields of an object (like those in the equals method in your toString method.

Avoid this, of course, if they will make it confusing or violate encapsulation. Never put data in your toString method that users may come to depend on because you don't provide an accessor to it.

Previous pageContentsNext page