4

Is there way how to get instance number or id, I mean that what you can see in eclipse Debug>Variables window after class name in value column.

Thanks

liborw
  • 842
  • 1
  • 8
  • 22
  • I believe that number is just the memory address of the object. `hashCode` might return this number, or a munged version of it. – cdhowie Nov 25 '10 at 20:01
  • possible duplicate of [Java object ID in jvm](http://stackoverflow.com/questions/3289550/java-object-id-in-jvm) – nos Nov 25 '10 at 20:03
  • 1
    I don't mean memory address, I mean the number of instance not object. It is possible that the number is generated by eclipse. – liborw Nov 25 '10 at 20:41

4 Answers4

7

See System.identityHashCode which returns the number that would be returned if Object.hashCode had not been overridden on an object. It is not a perfect proxy for identity since multiple objects might have the same identity hash code, but it is pretty useful for debugging.

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
1

By default, debugger shows you the result of toString() method with the hashCode of the object. So, you can see the value by executing hashCode() method.

Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
0

I suppose you're refering to Objects toString() implementation.:

return getClass().getName() + "@" + Integer.toHexString(hashCode());
stacker
  • 68,052
  • 28
  • 140
  • 210
0

Maybe I'm wrong, but I don't think instances of a class are numbered. I have searched many times for a way to get instances of a particular class and according to my searches, it would be impossible. Instances have an address, but I see no use for a class instance number (objects are not sorted by class in memory AFAIK). Note that you can always create a mechanism for giving numbers to you instances of a particular class. Have a static count in the class and give its value to each new instance you create (in the constructor) and increment it each time. That would be a way to differentiate instances.

Joanis
  • 1,669
  • 3
  • 20
  • 32