Metoda String

class Caine {
     
    private int id;
    private String nume;
     
    public Caine(int id, String nume) {
        this.id = id;
        this.nume = nume;
    }
     
    public String toString() {
         
        return String.format("%-4d: %s", id, nume);
         
        /*
        StringBuilder sb = new StringBuilder();
        sb.append(id).append(": ").append(name);
         
        return sb.toString();
        */
    }
}
 
public class App {
 
    public static void main(String[] args) {
        Caine caine1 = new Caine(1, "Laika");
        Caine caine2 = new Caine(2, "Puffy");
         
        System.out.println(caine1);
        System.out.println(caine2);
    }
}
Output is:
1   : Laika
2   : Puffy