explain the rules for tostring methods tostring


Explain the Rules for toString() Methods ?

toString() methods should return a single line of text that does not contain any carriage returns or linefeeds.
toString() methods are primarily for debugging.
toString() should not do a lot of fancy processing. toString() methods should be fast.

The string returned through toString() should contain the name of the class, and names and values of the fields which represent the state of the object, unless there are an excessive number of such fields, in that case only the most important should be returned.

A better Car toString() method would be:
public String toString() {
return "[Car: plate=" + this.licensePlate
+ " speed=" + this.speed + "; MaxSpeed=" + this.maxSpeed +"]");
}
These rules are conventions, not needs of the language.

Request for Solution File

Ask an Expert for Answer!!
JAVA Programming: explain the rules for tostring methods tostring
Reference No:- TGS0284741

Expected delivery within 24 Hours