Implements toString() method

Implements toString() method

Default implementation

Every class inherits methods from the Object class, including the toString() method. By default, the toString() method displays the class name and a simple hashCode.

However, for technical reasons (logging, tests, external components…), we should define our implementation for each class.

Let's take a class User:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public class User {

    private String firstname;
    private String lastname;

    public User(String firstname, String lastname) {
        this.firstname = firstname;
        this.lastname = lastname;
    }
}

And now, let's use the default implementation of the toString() method:

1
2
3
4
5
6
7
public class MyApplication {

    public static void main(String[] args) {
        User ronaldo = new User("Cristiano", "Ronaldo");
        System.out.println(ronaldo.toString());
    }
}

The output of this program is:

1
com.javaletsgo.User@6bdf28bb

As we can see, it prints the class name and a simple hashCode, which is not really useful in some context.

Every developers should understand that the default implementation of toString() has been called, because we didn't override it in the User class. The default implementation of toString() looks exactly like this:

1
2
3
4
// toString() implementation in the Object.class
public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

Override toString() implementation

Instead, let's print the first name and last name of the user. We need to override the toString() method in the User class:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
public class User {

    private String firstname;
    private String lastname;

    public User(String firstname, String lastname) {
        this.firstname = firstname;
        this.lastname = lastname;
    }
    
    // it will display the firstname and lastname
    @Override
    public String toString() {
        return firstname + " " + lastname;
    }
}

Let's run the program again:

1
2
3
4
5
6
7
public class MyApplication {

    public static void main(String[] args) {
        User ronaldo = new User("Cristiano", "Ronaldo");
        System.out.println(ronaldo.toString());
    }
}

The result should be:

1
Cristiano Ronaldo

We don't have to call toString() everytime

Let's remove the explicit call to the toString() method:

1
2
3
4
5
6
7
8
9
public class MyApplication {

    public static void main(String[] args) {
        User ronaldo = new User("Cristiano", "Ronaldo");
        
        // don't call toString() method
        System.out.println(ronaldo);
    }
}

Let's run the program again:

1
Cristiano Ronaldo

Why should the result be the same ? It's because under the hood, System.out.println() already calls the toString() method on the object User.

A lot of libraries has the same behaviour, like logging libraries (log4j, slf4j, Flogger…). That's why we don't have to always call the toString() method.