Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Internal Method Call ( toString)

 
RAGU KANNAN
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Folks,

I am New to Java, for the following example there is no call for the toString method.
But it's called internally, Pls let me know how it's called.

Thanks, Kannan.


class Bob {
int shoeSize;
String nickName;

public static void main (String[] args) {
Bob f = new Bob("GoBobGo", 19);
System.out.println(f);
}

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);
}

}
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever you use an object reference in a method like println, automatically toString is called and what it returns is printed.
 
mohamed ewis
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi RAGU
when calling System.out.println(f);
the println method check the Object passed to it here is f which is instance of blob if it equals null it print null if it not null it call the tostring() method of implemented in the blob class if it foun that the method not implemented in your class it print the refence variable of f

see the implementation of the println method in the source of your java

jdkXX directory/src.jar
[ March 21, 2006: Message edited by: mohamed ewis ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic