It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question required Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question required " Watch "question required " New topic

question required

Inuka Vincit
Ranch Hand

Joined: Aug 10, 2004
Posts: 175
I think your wondering why toString is called. When you called print or prinln on an object the toString method of that object is called.
Here is the doc


public void print(Object obj)

Print an object. The string produced by the String.valueOf(Object) method is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

obj - The Object to be printed
See Also:

here is the link to the doc

MCP (C# application dev 70-316) 860<br />SCJP 1.4 100% SCJD (URLyBird) 378<br />MAD 100% nuts
Ranch Hand

Joined: Aug 28, 2004
Posts: 70
class A {
public String toString () {
return "4";
class B extends A {
public String toString () {
return super.toString() + "3";
public class test {
public static void main(String[] args) {
System.out.println(new B());
Compilation succeeds and 43 is printed.
But why ?
Thanks !!
Colin Fletcher
Ranch Hand

Joined: Sep 10, 2004
Posts: 200
return super.toString() + "3"

Tells the class to call the super (A) toString method, which returns "4". "4" + "3" = "43"

SCJP 1.4 SCWCD 1.4
natarajan raman

Joined: Apr 10, 2004
Posts: 26
Thanks a lot!!!That's really helpful Inuka.....

nata.r<br />**********************************<br />To win is not always success<br />And to lose is not always failure.<br />**********************************

Joined: Sep 26, 2004
Posts: 13
I agree. Here's the link:
subject: question required
It's not a secret anymore!