Link to home
Start Free TrialLog in
Avatar of rnicholus
rnicholus

asked on

Class class toString method

I noticed that calling the overridden toString on a class (this) constructs a String similar to:

"ClassName@1cb25f1"

My question is, what is the data provided after the "@" sign in the returned String?  What does it repsent?  What would cause this portion of the String to change?

Avatar of aozarov
aozarov

This a memory reference value "1cb25f1".
calling toString on an object (without overriding it) will give a unique value for each instance.
Read http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Object.html#toString()
which provides the default implementation for toString
Nothing to care about. It's just the reference number of your object.
Avatar of rnicholus

ASKER

Actually, according to the API, it is a hashcode, which, I beleive (correct me if I'm wrong) will only change if the object itself changes.  My new question is, now, is this hashcode generated based simply on method signatures and class variable signatures, or will the hashcode change if a single character, say, inside of a method changes?
ASKER CERTIFIED SOLUTION
Avatar of aozarov
aozarov

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Actually, I do care about this number.  I'm seeing if I can use it to uniquely identify a class.  I need to know, however, how this number is exactly generated.  Such as how closely related the number is to the implementation of the class.
Thank you, once again aozarov (you have answered at least one of my other questions).  I used to examine API's before asking questions, but I've been lazy lately.  I'll try to correct that.

To sum up my conclusion, the string returned by an Object's toString method is not guaranteed to be the same from one execution to another, so it should not be used to uniquely identify an implementation of an object.
>>  not guaranteed to be the same from one execution to another, so it should not be used to uniquely identify an implementation of an object.
Correct. :-)
Have a look at Jakarta Commons lang project http://jakarta.apache.org/commons/lang/ which can assist you with a toString generation.
http://dev2dev.bea.com/lpt/a/406