Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get address of a Java Object? [duplicate]

Is there a way to get address of a Java object?

Where the question comes from?: At First, I read properties file and all the data from file was placed into table. Properties file can update. So, I want to listen that file. I listen an object using PropertyChangeSupport and PropertyChangeListener.

updatedStatus = new basit.data.MyString();
    updatedStatus.addPropertyChangeListener(new java.beans.PropertyChangeListener() {

        //After changes "i", we inform the table model about new value
            public void propertyChange(PropertyChangeEvent evt) {
                Object objec=evt.getNewValue();
                tableModel.setValueAt(objec.toString(), 0, 5);
            }
        });

If updatedStatus changes then i update table. MyString class have private String "Value". I want to listen properties file. So, it should make updatedStatus.value and String of Properties File equal at the same address. If i can do it, so i don't need to listen properties file.

updatedStatus.setValue(resourceMap.getString("HDI.Device.1.Name"));

I tried to use StringBuffer, but i couldn't achieve it. That's why, I asked the question.

like image 956
Iguramu Avatar asked Sep 01 '09 06:09

Iguramu


2 Answers

Firstly - no, you can't get the address of an object in Java; at least, not pure Java with no debugging agent etc. The address can move over time, for one thing. You don't need it.

Secondly, it's slightly hard to follow your explanation but you certainly won't be able to get away without listening for changes to the file itself. Once you've loaded the file into a Properties object, any later changes to the file on disk won't be visible in that object unless you specifically reload it.

Basically you should listen for changes to the file (or poll it) and reload the file (either into a new Properties or overwriting the existing one) at that point. Quite whether you also need to listen for updates on the string container will depend on your application.

like image 169
Jon Skeet Avatar answered Oct 22 '22 00:10

Jon Skeet


System.identityHashCode(obj) delivers the next-best thing: a number unique for each object. It corresponds to the default Object.hashCode() implementation.

To quote the API: "As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)".

like image 42
mfx Avatar answered Oct 21 '22 23:10

mfx