Meaningless Drivel is fun!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Internal Method Call ( toString) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Internal Method Call ( toString)" Watch "Internal Method Call ( toString)" New topic

Internal Method Call ( toString)

Ranch Hand

Joined: Dec 16, 2005
Posts: 103
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);

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

Joined: Feb 07, 2005
Posts: 2398
Whenever you use an object reference in a method like println, automatically toString is called and what it returns is printed.
mohamed ewis

Joined: Feb 05, 2006
Posts: 18
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 ]
I agree. Here's the link:
subject: Internal Method Call ( toString)
jQuery in Action, 3rd edition