HOWTO · Java
How to Override the toString Method in Java
This article explains how to override the toString() method in java
On this page
When Java prints an object, it calls that object’s toString() method to get text. The inherited default usually identifies the object’s class and a hash-based value, which can help distinguish instances but rarely explains their contents. Override toString() to provide a concise representation that is useful in logs, debugging, and ordinary output.
What the default toString() output means
Every Java class inherits toString() from Object, unless a superclass or the class itself overrides it. Calls such as System.out.println(book) use the object’s string representation; the call is implicit, so you do not need to write book.toString() yourself.
The default representation is not a stable identifier or a promise about an object’s fields. Its hash-based portion can differ between runs, and it does not make the object’s state readable. Avoid copying a sample default value into application logic or tests. A custom override is appropriate when a class should have a more informative text representation.
Override toString() with @Override
Declare a public method with the exact signature public String toString() and return a String. The @Override annotation tells the compiler that this method is intended to replace an inherited method; it also helps catch a misspelled name or incorrect parameter list. This complete example stores a book’s title and author and builds its representation from those fields:
Run this example:
public class BookExample {
public static void main(String[] args) {
Book book = new Book("Dune", "Frank Herbert");
System.out.println(book);
}
}
class Book {
private final String title;
private final String author;
Book(String title, String author) {
this.title = title;
this.author = author;
}
@Override
public String toString() {
return "Book{title='" + title + "', author='" + author + "'}";
}
}
Compile and run the example
Save the source in a file named BookExample.java, since the public class is named BookExample. From that directory, compile it with javac BookExample.java, then run it with java BookExample. The successful run prints Book{title='Dune', author='Frank Herbert'}, the representation returned by Book.toString().
The key distinction is that toString() returns a string; it does not print anything on its own. System.out.println(book) asks for the representation and writes it to the console. You can also use the returned string in a logger or another message, while keeping display or logging decisions outside the method.
Choose useful toString() output
Include fields that help a reader recognize the object and understand its current state. Keep the format concise, label values when that improves clarity, and omit secrets such as passwords or access tokens. A representation is intended for people, so choose stable, understandable values rather than relying on the inherited hash-based text.
The method should report state rather than perform unrelated work: avoid changing fields, printing messages, or doing expensive operations while constructing the string. When a class has many fields, include the ones most useful for diagnosis instead of returning an unwieldy dump. If exact formatting is part of a public file or network protocol, define and test that format separately; callers should not have to parse a debugging representation as a data contract.