Learn Java Tutorial for Beginners (Video), Part 21: toString()

How to use the important toString() method in Java, and a bit about the Object class that's the grandfather of all objects in Java.

When the video is running, click the maximize button in the lower-right-hand corner to make it full screen.

Code for this tutorial:

class Frog {
    private int id;
    private String name;
    public Frog(int id, String name) {
        this.id = id;
        this.name = name;
    public String toString() {
        return String.format("%-4d: %s", id, name);
        StringBuilder sb = new StringBuilder();
        sb.append(id).append(": ").append(name);
        return sb.toString();

public class App {

    public static void main(String[] args) {
        Frog frog1 = new Frog(7, "Freddy");
        Frog frog2 = new Frog(5, "Roger");

7   : Freddy
5   : Roger