• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Getting the Object address

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic