5 Replies Latest reply: Jun 22, 2010 2:41 PM by aksarben RSS

    When and why do we override toString() method

    843789
      public class BobTest {
              public static void main (String[] args) {
                Bob f = new Bob("GoBobGo", 19);
                System.out.println(f); // if i put s.o.p("hello"); the overridden toString doesnt get called, y so ?
              }
           }
           class Bob {
              int shoeSize;
              String nickName;
              Bob(String nickName, int shoeSize) {
                this.shoeSize = shoeSize;
                this.nickName = nickName;
              }
              public String toString() {
                 return ("I am a Bob, but you can call me " + nickName +
                         ". My shoe size is " + shoeSize);
              }
           }