Override .toString() Method Tutorial

Throughout my tutorials I have discussed the toString() method on numerous occasions, so I thought it would be a good idea to give this popular method its own tutorial. Out of all 11 methods in the Object class, toString() is perhaps the easiest method to understand. Of course every class inherits the toString() method from the Object class and by default it returns a string value that represents the instance in a stringy sort of way. In order to understand precisely what the result of the default toString() method returns you should first watch my tutorials on Object .getClass() and Class Common Methods and my Overriding the .hashCode() method Tutorial.

Consider the following code:
class Blah {
}
class Tester {
     public static void main(String args[]){
        System.out.println(new Blah());
        System.out.println(new Blah().toString());
    }
}
The result of the above code will produce something like Blah@2a139a55 (your hex values will vary) twice. Each result is the default stringy sort of description of the object instance. Notice how the first call to the println() method did not explicitly invoke the toString() method. That is because whenever you pass just an object to the println() method, the toString() method is invoked implicitly.

Overriding the toString() method is a very simple process. Simply add the following code into the Blah class.
class Blah {
     @Override
// good practice in case you misspell or forget a modifier
     public String toString() {
         return "Whatever you want";
     }
}
Now the result will be "What ever you want" when the toString() method is invoked, but the real purpose of the toString method is to return something useful. Generally speaking, the most useful result from invoking the toString() method is to display the current state (instance variable value).
class Blah {
     private int age = 41;
     private boolean interesting = false;
     @Override
// good practice in case you misspell or forget a modifier
     public String toString() {
         return "age = " + age + ", interesting = " + interesting;
     }
}
Now the result will be "age = 41, interesting = false" when the toString() method is invoked. The toString() method is used quite often for simply displaying the current state, especially when private variables are involved (proper encapsulation). One final point to emphasize ... should you, as a programmer, ever try to parse out the result of the toString() method to read values that otherwise may not be accessible? That answer is a resounding Bad Idea! The toString() method should never be relied upon for parsing instance variable values; there is no guarantee that future releases of a class will return the string result in the same format. That is the purpose of getter() and setter() methods, not toString(). The toString() is only meant to be informative, not drive logic or change variables. Let's dig a little deeper into the toString() method in the source code below.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md OverrideToString
C:\Java>cd OverrideToString
C:\Java\OverrideToString>Notepad OverrideToString.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class Blah { 
}

class OverrideToString {
    public static void main(String args[]){
        System.out.println(new Blah().toString());
    }
}

Now switch back to the command prompt (CMD) and type in javac OverrideToString.java and press Enter.
Now type in java OverrideToString and press Enter.


C:\Java\OverrideToString>javac OverrideToString.java
C:\Java\OverrideToString>java OverrideToString
Blah@2a139a55


Final thoughts

None


Tutorials