Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes toString() method 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 » Java » Beginning Java
Bookmark "toString() method " Watch "toString() method " New topic
Author

toString() method

jhon max
Ranch Hand

Joined: Jan 06, 2010
Posts: 45


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

Joined: Jan 08, 2010
Posts: 5
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

Joined: Jan 06, 2010
Posts: 45
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

Joined: Jan 08, 2010
Posts: 5
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

Joined: Feb 10, 2009
Posts: 710
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.

SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
jhon max
Ranch Hand

Joined: Jan 06, 2010
Posts: 45
hmm this is a bit confusing and for me ryt now... anyway tnx for the answers!!!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

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

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
jhon max
Ranch Hand

Joined: Jan 06, 2010
Posts: 45
oh sorry sir... wont happen again.. anywany thanks for letting me know about that,,

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: toString() method
 
Similar Threads
Problem Using toString method from a different class
How to display sum of numbers from a java class to a jsp using custom tag?
Large Numbers
[HOMEWORK] Please need help with java project
int can not be dereferenced error