*
The moose likes Beginning Java and the fly likes Getting the Object address 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 "Getting the Object address" Watch "Getting the Object address" New topic
Author

Getting the Object address

James Davison
Greenhorn

Joined: Mar 28, 2004
Posts: 27
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.
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

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.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
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 ]

Mike Gershman
SCJP 1.4, SCWCD in process
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting the Object address
 
Similar Threads
Hashcode doubt
User-defined objects in collections
overriding
Address of object?
object Array Reference casting