Java Arena
Step 3 of 7

toString

Printing an object

To show a plant, the Plant of module 17 had a method printInfo(). It works, but the text can only go straight to the screen, on a line of its own: it can't be part of a longer line, or be stored in a variable. What about printing the object itself, with System.out.println(basil)? It compiles, but for a class like Plant it prints the class name, an @ and a code of letters and digits, something like Plant@1b6d3586. The code is an internal number that means nothing to a reader, and it may differ between runs and between computers. That's why no check in this course ever shows it.

toString

A class decides how its objects look as text with a method called toString. It has no parameters and returns a String:

Main.java
public class Main {
    public static void main(String[] args) {
        Ferry ferry = new Ferry("Aurora");
        System.out.println(ferry);
        ferry.board(12);
        System.out.println(ferry);
        String line = "Departing: " + ferry;
        System.out.println(line);
    }
}
Ferry.java
public class Ferry {
    private String name;
    private int cars;

    public Ferry(String name) {
        this.name = name;
        this.cars = 0;
    }

    public void board(int count) {
        this.cars = this.cars + count;
    }

    public String toString() {
        return "Ferry " + this.name + ", " + this.cars + " cars on board";
    }
}
Output
Ferry Aurora, 0 cars on board
Ferry Aurora, 12 cars on board
Departing: Ferry Aurora, 12 cars on board
  • The header must be exactly public String toString(). With another name, or with a parameter, println won't use it.
  • System.out.println(ferry) calls ferry.toString() for you and prints the text it returns.
  • Joining an object to a String with + calls it too: "Departing: " + ferry is "Departing: " followed by what toString returns.
  • The text is built each time the method is called, from the object's variables at that moment. After board(12) it shows the new number of cars.

toString returns the text; it doesn't print it. Printing is up to the code that calls it, so the same text can be printed, stored in a variable or joined into a longer line. Like any method that returns a String, toString can use if and build its text in steps.

Your task

Challenge 1 of 3

Add a toString to the Guitar class, so a guitar prints as its model, with its number of strings in parentheses. The check creates new Guitar("Lark", 6) and prints it:

Lark (6 strings)

Challenge 2

A Chore on a to-do list is either done or not. Add a toString that returns [ ] followed by the chore's title while it's not done, and [x] followed by the title once it's done. The check prints a new chore, marks it done and prints it again:

[ ] Water the plants
[x] Water the plants

Challenge 3

What does this program print? Watch when each toString call happens.