wood burning stoves 2.0*
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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Internal Method Call ( toString)" Watch "Internal Method Call ( toString)" New topic
Author

Internal Method Call ( toString)

RAGU KANNAN
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);
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

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

Joined: Feb 05, 2006
Posts: 18
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 ]
 
jQuery in Action, 2nd edition
 
subject: Internal Method Call ( toString)
 
Similar Threads
Bad Form to name indentifier and method the same?
strange result
how toString() works in this program???
toString() method explaination
Which one is better