As I've been learning and even some IDEs have it embeded in it, to override the toString()
method to print out all instance variables of the class.
The original toString()
defined in object.java
is defined as follows:
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
And it's common practice to override it as:
public String toString() {
return "className{" +"var1="+var1+", var2="+var2+'}';
}
Why don't we keep the original functionality of the toString()
method and create a new method (with a different name) with this functionality?