By Doug Lowe

The toString method returns a String representation of an object in Java. By default, the toString method returns the name of the object’s class plus its hash code. Here, you find out how to use the toString method and how to override it in your own classes to create more useful strings.

Using toString in Java

Here’s a simple program that puts the toString method to work:

public class TestToString

{

public static void main(String[] args)

{

Employee emp = new Employee(“Martinez”,

“Anthony”);

System.out.println(emp.toString());

}

}

class Employee

{

private String lastName;

private String firstName;

public Employee(String lastName, String firstName)

{

this.lastName = lastName;

this.firstName = firstName;

}

}

This code creates a new Employee object; then the result of its toString method is printed to the console. When you run this program, the following line is printed on the console:

Employee@82ba41

Note: The hash code — in this case, 82ba41 — will undoubtedly be different on your system.

It turns out that the explicit call to toString isn’t really necessary in this example. You could just as easily have written the second line of the main method like this:

System.out.println(emp);

That’s because the println method automatically calls the toString method of any object you pass it.

Overriding toString in Java

The default implementation of toString isn’t very useful in most situations. You don’t really learn much about an Employee object by seeing its hash code, for example. Wouldn’t it be better if the toString method returned some actual data from the object, such as the employee’s name?

To do that, you must override the toString method in your classes. In fact, one of the basic guidelines of object-oriented programming in Java is to always override toString. Here’s a simple program with an Employee class that overrides toString:

public class TestToString

{

public static void main(String[] args)

{

Employee emp = new Employee(“Martinez”,

“Anthony”);

System.out.println(emp.toString());

}

}

class Employee

{

private String lastName;

private String firstName;

public Employee(String lastName, String firstName)

{

this.lastName = lastName;

this.firstName = firstName;

}

public String toString()

{

return “Employee[“

+ this.firstName + “ “

+ this.lastName + “]”;

}

}

When you run this program, the following line is displayed on the console:

Employee[Anthony Martinez]

Note that the output consists of the class name followed by some data from the object in brackets. This convention is common in Java programming.

The only problem with the preceding example is that the class name is hard-coded into the toString method. You can use the getClass method to retrieve the actual class name at runtime:

public String toString()

{

return this.getClass().getName() + “[“

+ this.firstName + “ “

+ this.lastName + “]”;

}

Here the getClass method returns a Class object that represents the class of the current object. Then the Class object’s getName method is used to get the actual class name.