• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Why does this java code do this?

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I'm trying to understand why the following code calls the local toString() method and not the testMethod()?


This prints out the string "43" as a result.

Even when I change the name of the method in the super class to toFString() it prints out the same returned string?
Why does the local toString() method even get called, I just ruled out it being because the super method had the same name.
...and why isn't it calling the testMethod() as well...???
Any help on understanding this would be greatly appreciated.

Regards,
Java Joe
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joe McTigue wrote:I'm trying to understand why the following code calls the local toString() method and not the testMethod()?


And I'm trying to understand why you think it would call testMethod(), since you never call it yourself.

There's a very good reason why it would call toString() though, which you can find in the docs for String.valueOf(Object) (which is called by println(Object)).

HIH

Winston
 
Joe McTigue
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Winston,

So the toString() local method is called by default from println() when println() is asked to print the object.
Thanks for explaining this and adding the reference. :-)

Thanks,
Java Joe
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Joe

The toString() method is provided by the Object class so that each object can be represented meaningfully if you do not override it then it may spit out smething like, "SubClass@100ab23". In your program you are using System.out.println(new SubClass()) which will invoke the toStirng() method of the SubClass. Inside toStirng() using super.toString() which in will further call the toString() method of the Super class which returen 4 and inside SubClass's toString() method a 3 is appended to it and 43 is printed.

The main point is whenever you try to use an object or reference variable of an object inside the println() method it will make an implicit call to object's toString() method of that object.

 
Joe McTigue
Ranch Hand
Posts: 73
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Ikneet Singh,

I really appreciated your description on why and how this works, it really helped me understand the process better.

Regards,
Joe
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ikneet singh wrote:The main point is whenever you try to use an object or reference variable of an object inside the println() method it will make an implicit call to object's toString() method of that object.



It's not implicit. System.out is a PrintStream. This is from PrintStream.java: and this is the method it calls in the String class:
 
ikneet singh
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Darryl,

Thanks for the correction . Actually, I was referring to the same API which you mentioned, but I guess I was unable to make it clear thanks for doing so. The toString() method is called by the valueOf() method of the String class if the object passed is not null.
Cheers.

Regards,
Ikneet Singh
reply
    Bookmark Topic Watch Topic
  • New Topic