public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "Name: " + name + ", Age: " + age; } } Person person = new Person("John Doe", 30); System.out.println(person.toString());
Name: John Doe, Age: 30
public class Rectangle { private int width; private int height; public Rectangle(int width, int height) { this.width = width; this.height = height; } } Rectangle rect = new Rectangle(10, 20); System.out.println(rect.toString());
Rectangle@1bdbd4f0This code is not part of any library package.