Override the toString() method in your classes : toString method « Utility Classes « SCJP






public class MainClass {
  public static void main(String[] args) {
    MyClass f = new MyClass("A", 19);
    System.out.println(f);
  }
}

class MyClass {
  int shoeSize;

  String nickName;

  MyClass(String nickName, int shoeSize) {
    this.shoeSize = shoeSize;
    this.nickName = nickName;
  }

  public String toString() {
    return ("Call me " + nickName + ". My shoe size is " + shoeSize);
  }
}
Call me A. My shoe size is 19








8.3.toString method
8.3.1.The purpose of the toString() method is to provide a string representation of an object's state.
8.3.2.Override the toString() method in your classes