Back to Java
2026-01-285 min read

toString()

Learn toString() step by step with clear examples and exercises.

Why This Matters

Understanding and effectively utilizing the toString() method in Java is essential for various reasons:

  1. Debugging: The toString() method helps developers to easily understand the state of their objects during runtime, making debugging more efficient.
  2. String Concatenation: When concatenating strings with objects, the toString() method is automatically called to convert the object into a string format.
  3. Object Representation: The toString() method provides a human-readable representation of complex objects, making it easier to work with them.
  4. Consistency: By providing a consistent string representation for all objects, it simplifies code readability and maintenance across an application.

Prerequisites

To fully appreciate this tutorial, you should have a strong foundation in the following areas:

  1. Java basics (variables, methods, classes)
  2. Object-oriented programming concepts (objects, instances, classes)
  3. String manipulation in Java
  4. Understanding of inheritance and polymorphism
  5. Familiarity with Java's default toString() method behavior

Core Concept

The toString() method is a predefined method available for all objects in Java. By default, it returns the class name and memory location of the object. However, we can override this method to provide a custom string representation that accurately describes the state of our objects.

Overriding toString()

To override the toString() method, create a new method with the same signature (public String toString()) within your class:

public class MyClass {
private int id;
private String name;

public MyClass(int id, String name) {
this.id = id;
this.name = name;
}

@Override
public String toString() {
return "MyClass{id=" + id + ", name='" + name + "'}";
}
}

In the example above, we've created a simple class called MyClass with two private fields: id and name. We've also overridden the toString() method to return a custom string representation of the object.

Worked Example

Let's create a more complex application that demonstrates the usage of the toString() method:

public class Main {
public static void main(String[] args) {
MyClass myObject = new MyClass(1, "John Doe");
System.out.println(myObject); // Output: MyClass{id=1, name='John Doe'}

Person person = new Person("Alice", 30, "123 Main St.");
System.out.println(person); // Output: Person{name='Alice', age=30, address='123 Main St.'}
}
}

In this example, we've created an instance of MyClass and a new class called Person. We've overridden the toString() method for both classes to return a custom string representation of their respective objects.

Common Mistakes

  1. Forgetting to override toString(): If you don't override the toString() method, Java will use its default implementation, which may not be useful for your specific object.
  2. Returning an incorrect or misleading string: Ensure that the returned string accurately represents the state of the object and is easy to understand.
  3. Not updating toString() when changing class fields: If you add, remove, or modify class fields, make sure to update the toString() method accordingly.
  4. Ignoring the importance of toString() in debugging: The toString() method can be incredibly helpful when debugging your code, so don't neglect it!
  5. Not formatting the string properly: Properly format your strings to ensure they are easy to read and understand.
  6. Returning sensitive information: Be careful not to return sensitive information in your toString() method output.
  7. Using non-final fields in toString(): Avoid using non-final fields in the toString() method, as their values may change during object creation, leading to inconsistent string representations.

Practice Questions

  1. Override the toString() method for a class called Car, which has fields for make, model, year, and color.
  2. Given the following classes:
public class MyClass {
private int id;
private String name;

public MyClass(int id, String name) {
this.id = id;
this.name = name;
}
}

public class Person {
private String name;
private int age;
private String address;

public Person(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
}

What will be the output of the following code snippet:

MyClass myObject = new MyClass(1, "John Doe");
Person person = new Person("Alice", 30, "123 Main St.");
System.out.println(myObject);
System.out.println(person);

Answer: The output will be:

MyClass{id=1, name='John Doe'}
Person{name='Alice', age=30, address='123 Main St.'}

FAQ

  1. Why should I override toString()? Overriding the toString() method allows you to provide a custom string representation of your object, making it easier to understand and debug your code.
  2. What happens if I don't override toString()? If you don't override the toString() method, Java will use its default implementation, which may not be useful for your specific object.
  3. Can I return an empty string from toString()? While it's technically possible to return an empty string, this can lead to confusion when debugging or logging your objects. It's generally better to provide a meaningful string representation.
  4. How do I override toString() for a final class? You cannot directly override the toString() method in a final class because it is declared as final itself. However, you can create an inner class within the final class and override the toString() method there.
  5. What should I include in my toString() method? Include relevant information about the object's state that helps to identify and understand the object when debugging or logging.
  6. Should I format my strings in the toString() method? Yes, properly formatting your strings can make them easier to read and understand.
  7. What should I avoid when formatting my strings in toString()? Avoid using non-final fields directly in the toString() method, as their values may change during object creation, leading to inconsistent string representations.
toString() | Java | XQA Learn