如何获取Java对象的地址? [英] How to get address of a Java Object?

查看:361
本文介绍了如何获取Java对象的地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法获取Java对象的地址?

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

问题来自哪里?:
首先,我读取属性文件,文件中的所有数据都放入表中。属性文件可以更新。所以,我想听那个文件。我使用PropertyChangeSupport和PropertyChangeListener来监听对象。

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);
            }
        });

如果updatedStatus更改,那么我更新表。 MyString类具有私有字符串Value。我想听属性文件。因此,它应该使updatedStatus.value和属性文件的字符串在相同的地址相等。如果我能做到,所以我不需要监听属性文件。

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

我试图使用StringBuffer,但我无法实现它。这就是为什么,我问了这个问题。

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

推荐答案

首先 - 不,你不能用Java获取对象的地址;至少,不是没有调试代理等的纯Java。一方面,地址可以随着时间的推移而移动。你不需要它。

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.

这篇关于如何获取Java对象的地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆