tags:

views:

173

answers:

3

I can get the unique id like className@2345 of my object by calling its toString() method, but after I overwrite the toString() method, how can I get that unique id?

+12  A: 

You can call System.identityHashCode() and pass your object as parameter, then you will get it.

Rose
A: 

If you are looking for a one line toString() implementation that will also print the className@address, check out Apache Commons ToStringBuilder.reflectionToString(). This will return a String in the format: className@address[field1=value1, field2=value2, ...]

0sumgain
It's not an address but a hash code.
joeslice
+2  A: 

More precisely

obj.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(obj))
sjlee
Or justpublic String identityString() { return super.toString();}?
Cowan