0

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?

  • 3
    This could be useful for basic debugging and logging. Which would you rather see printed in a console or log statement when trying to figure out the state of your application? The reason toString is overridden v.s. using a new method is because toString gets called when an object is evaluated as a string. For example `System.out.println(object)` is going to call the toString method. – secondbreakfast Oct 27 '20 at 15:03
  • "override the method toString() in java to *print* the instance variables of the class?" nitpicking but `toString` shouldn't be *printing* anything. It should *return* string representing state of an object (its data, and what it represent). How and when it should be printed is up to user of the class. – Pshemo Oct 27 '20 at 15:09
  • One of the advantages of oop is polymorphism. Why throw it away? By overriding toString(), it will appear this way everywhere toString() is called. – NomadMaker Oct 27 '20 at 16:25

2 Answers2

0

We could. But how will other classes, that know absolutely nothing about your subclasses with your myNewToString method, know how to print a string that textually represents, in a concise but informative way, arbitrary subclasses?

The toString method was designed to be overridden to do that. Yes, it does have default behavior but it's not very useful. Its authors wanted you to override it. Overriding it to return what's commonly practiced is more useful, but you don't have to do that. A toString method for an EmailAddress class can return

public String toString() {
    return "EmailAddress{localPart = " + localPart + ", domainName = " + domainName + "}";
}

but it's usually more useful to return something like

public String toString() {
    return localPart + "@" + domainName;
}
Juan C Nuno
  • 476
  • 1
  • 3
  • 8
0

The reason to override toString() is that toString() is called implicit by the compiler every time an object which is not of type String, is added to a string. So if you have

MyObject o=new MyObject();
C="Hello " + o;

Then the compiler will call o.toString() in order to get a string it can concat to "Hello "

And I should note that it checks if o is null before calling toString() on o. And if o is null, it just generate the string "null"

Opinion: The toString() method should in general and in most cases only be used for debugging (Print/Log) and not as part of the normal program flow.

MTilsted
  • 5,425
  • 9
  • 44
  • 76