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

Home
SCJP
1.Java Source And Data Type
2.Operators
3.Modifiers
4.Type Casting
5.Statements
6.Object Oriented
7.Thread
8.Utility Classes
9.File
SCJP » Utility Classes » toString method 
8.3.2.Override the toString() method in your classes
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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.