This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

toString() method

 
jhon max
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


when im not yet Overriding the toString() method the output is
this:


but when i include the toString() method
the output became fine:


but i only added the toString() method... i didnt use this method in any of my predefined methods? how does it affects the correct output?

by the way, to be honest i've already posted this thread in other forums, but i need some more clarification of why this happened...
http://www.java-forums.org/new-java/24496-returning-object-method.html - cross posted

 
Jack Ploeg
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you don't override toString then the toString implementation of Object is used, since that's the only class your class extends. This returns a string that identifies the class of your object and somthing like an address.
 
jhon max
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you don't override toString then the toString implementation of Object is used, since that's the only class your class extends


so if i dont override the toString method properly, then my class will AUTOMATICALY extend as an object? and my Class
will use the Object class's toString() method to make a string representation of its object...?

---

in my case im reffering to the String class? not in the object class?


is that how the toString() of String class works and differs from the toString() method of Object class?
 
Jack Ploeg
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
EVERY class extends the Java base-class Object. Check here: Java Object So every class automatically inherits this Object class's implementation of toString. As you found out you can override this to do whatever you want it to, but if you don't then the default implementation is used.
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jhon max wrote:
if you don't override toString then the toString implementation of Object is used, since that's the only class your class extends


so if i dont override the toString method properly, then my class will AUTOMATICALY extend as an object? and my Class
will use the Object class's toString() method to make a string representation of its object...?

---

in my case im reffering to the String class? not in the object class?


is that how the toString() of String class works and differs from the toString() method of Object class?


Every class extends Object, no matter what. So if you call toString() on any class that doesn't override the method, it will walk up the hierarchy to find the most specific toString(). If there isn't one, it will use the Object class's toString() method.
 
jhon max
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm this is a bit confusing and for me ryt now... anyway tnx for the answers!!!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64708
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jhon max wrote:hmm this is a bit confusing and for me ryt now... anyway tnx for the answers!!!

Please use real words when posting to the forums. Abbreviations such as "ryt" in place of "right" and "tnx" for "thanks" only serve to make your posts more difficult to read and less likely to generate useful responses.

Please read this for more information.

thanks,
bear
JavaRanch sheriff
 
jhon max
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh sorry sir... wont happen again.. anywany thanks for letting me know about that,,

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic