Is there a way to get the address of an Object? I know the default toString() method on Object returns a value that is based on the Object address but what if the toString() method is overridden? I want to obtain a unique reference to instances sot that they may stored in and retrieve from a Hashtable. The qualified class name is insufficient because more than one instance of a class may be stored. I would appreciate any suggestions.
In Java, there is no garunteed way to obtain the address of an object. In fact, I believe that the default .toString() method is not actually specified to return any information relating to the address of an object. If you are writing your own Hashtable for a class project, just use the .hashCode() method of an Object. While it is not garunteed to be unique, it will suffice most of the time. And when it doesn't, that little implementation of hash codes is an exercise best left to the reader (Hint: it involves the .equals() method, which is why you should never override .equals() without also overriding .hashCode()) If you are not writing your own Hashtabl for a class project, why worry about it? Just use some standard implementation of java.util.Map, such as java.util.HashMap?
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Actually, the toString() method in Object does not return an address at all
The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object
(from J2SE 1.4.2 API Specification) There is a reference in the O'Reilly book "Java in a Nutshell" to a (presumably unique) eight digit hexadecimal ID number as an operand of the jdb print command. However, I could find no reference to this in the Sun Java doc, let alone a way to find this number from within a Java program. I don't think this could be allowed as it would be hard to enforce security and encapsulation in a program that could directly address fields. [ April 09, 2004: Message edited by: Mike Gershman ]