How to get the address of a Java object?

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

Where does the question arise ?: First, I read the properties file, and all the data from the file was placed in a table. The properties file may be updated. So, I want to listen to this file. I am listening to 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 updateStatus changes, I am updating the table. The MyString class has a private string "Value". I want to listen to the properties file. Thus, it should make updatedStatus.value and String of the properties file equal at the same address. If I can do this, so I don’t need to listen to the properties file.

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

I tried to use StringBuffer, but I could not achieve this. So I asked a question.

+6
java string stringbuffer
source share
6 answers

Firstly, no, you cannot get the address of an object in Java; at least not pure Java without a debugging agent, etc. An address can move over time, on the one hand. You do not need it.

Secondly, it’s a little difficult for you to follow your explanations, but of course you can’t leave without listening to the changes in the file itself. After you have loaded the file into the Properties object, any subsequent changes to the file on disk will not be visible in this object, unless you specifically reload it.

Basically, you should listen to the changes in the file (or poll it) and reload the file (either into the new Properties or overwrite the existing one) at this point. Just the same, whether you need to listen for updates in the string container will depend on your application.

+18
source share

we can get the address of the object in memory. Well how? This is true:

using sun.misc.Unsafe class in java.

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

and there are also many methods for this class.

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

Note: this class is truly UNSAFE . if you do the wrong thing in your project, the JVM will stop.

+5
source share

Java is not like C / C ++. in C ++, you will often work with an address (a C ++ programmer has a concept call pointer). But I'm afraid this is not in Java. Java is very secure, allowing you to touch its address.

But, other ways, perhaps the same as your idea, are to use HashCode. HashCode database of objects at their address in HEAP.

+2
source share

Check out the Apache Commons configuration. This library supports dynamic reloading (for example) of property files. See here .

+1
source share

Object.identityHashcode (obj) provides the following best thing: a number unique to each object. It corresponds to the default implementation of Object.hashCode ().

To quote the API: "As far as reasonably practical, the hashCode method defined by the Object class returns different integers for different objects (this is usually implemented by converting the internal address of the object to an integer, but this implementation method is not required by the JavaTM programming language.)."

+1
source share

The best way to observe if some file changes is IMHO is to make a hash value with sha1 or mda5 and store the value in the cache. And you create a stream that every minute, second depends on how often you watch the file changes, and make a hash value on the file. So you can compare these two values, and if the values ​​are not equivalent, you can reload the new file.

+1
source share

All Articles