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

6

22

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.

Reconcile answered 1/9, 2009 at 6:26 Comment(0)
S
27

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.

Sedda answered 1/9, 2009 at 6:37 Comment(1)
Yes, I agree with answer. If you still need to, there is an unconventional way to get the address of a java object using sun.misc.Unsafe api. Refer: javapapers.com/core-java/address-of-a-java-objectEloisaeloise
V
17

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.)".

Valenevalenka answered 1/9, 2009 at 7:5 Comment(3)
Object.hashCode and System.identityHashCode absolutely do not give unique numbers. There is an RFE to clarify the API docs.Reisman
Note that the description says "As much as is reasonably practical". There is no guarantee that Object.hashCode() returns a unique number for each object, a different JVM implementation might not do the same as Sun's JVM.Better
The GC can supposedly move objects around at any time, updating all references to them. So why does the standard implementation of System.identityHashCode supposedly use the address of objects, if objects can be moved?Cyanide
C
7

we can get address of an object in memory. Well how? it is like that;

using sun.misc.Unsafe class in java.

create new Unsafe object and use the getAddress(Object); method and it will return a long value that is address.

and also there are many methods for this class.

you can change the values in this address using putInt(Object,long offset, int value) or like this method.(getting some value getnt(Object)).

Note: this class is really UNSAFE . if you make wrong things on your project, JVM will be stopped.

Chamberlain answered 24/12, 2014 at 9:15 Comment(1)
actually getAddress accepts primitive long type value.Moppet
E
1

Look into Apache Commons Configuration. This library has support for dynamic reloading of (for example) property files. See here.

Epidemic answered 1/9, 2009 at 7:5 Comment(0)
E
1

The best way to observe if some file changes is IMHO to make a hash value with sha1 or mda5 and save the value in a cache. And you make a Thread that every minutes, seconds, depends how often you watch file changes, and make hash value over the file. So you can compare this two values and if the values are not equivalent so you can reload the new file.

Eldaelden answered 1/9, 2009 at 11:15 Comment(0)
B
0

Java not like C/C++. in C++, you will often work with address (that C++ programmer has a concept call pointer). But, I afraid that not in Java. Java is very safe that prevent you to touch its address.

But, there other ways maybe same with your idea is use HashCode. HashCode of an object base on their address on HEAP.

Breccia answered 28/3, 2012 at 9:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.