简体   繁体   中英

How can I get an Object's name in java?

like this, A a = new A(), how can I get a's name?(Get a String "a" from a) ?


There is a JPanel contains some JTextFields, a map contains all the JTextFields' names(the variables' names). I want to set the map's values to the JTextFields' texts.

public void mapToJPanel(Map map, JPanel panel) {
    Component[] compArr = panel.getComponents();
    for (Component comp : compArr) {
        if (comp.getClass().getSimpleName().equals("JTextField")) {
            JTextField textField = (JTextField) comp;
            textField.setText(map.get(textField.getName()).toString());//getName() method
        }
    }
}

Accross getName() method, I get null -_- I know the getName() method is not used to get the variable name. I'm using netbeans doing Java swing visual development , so I can not rewrite the components(like JTextField).

You cannot, because an object does not have a name. Consider the following for example:

A a = new A();
A b = a;

What is the "name" of the A instance now? Is it "a" ? Is it "b" ?

And what about this?

A[] a = new A[] { new A(), new A()};
a[0] = a[1];

What is the "name" of the instance at a[1] ?

My point is that you cannot come up with a useful / usable definition of a universal Java object name that works in a wide range of contexts. Roadblock issues include:

  • Name stability: a "name" that changes when an object's reference is assigned is not useful.
  • Single name: an object should not simultaneously have multiple names.
  • Implementability: any naming system would have to be implementable without imposing a performance cost on normal usage of objects. AFAIK, this is impossible ... at least on the kind of hardware we currently use.

The closest that Java comes to a name for an object is the object's "identity hashcode" value. It is not unique, but it has the property that it won't change for the lifetime of the object in the current JVM. (But even the identity hashcode comes at a runtime cost ... which makes it a Java design mistake in some peoples' view.)

The sensible approach to naming objects is (as others have said) to add "name" fields to the relevant classes (or use an existing one) and manage the names yourself as required.


In the more specific case where the "object" is actually a JComponent , a given component cannot be relied on to have a name. (The getName() method can return null .) However, if you wanted to, you could traverse any JComponent hierarchy and set a name on any components as appropriate.

You can't.

If you compile with debug symbols then the .class file will contain a table of variable names (which is how debuggers map variables back to your source code), but there's no guarantee this will be there and it's not exposed in the runtime.

您可以使用Component.setName()为Swing和AWT组件指定名称。

You can't and it seems like you're doing something wrong if you need to do this. However, if you really want to pursue the logic you've outlined, why don't you do the following:

Add a String member to A and in a constructor, assign it. Something like this:

 A a = new A('a');

You can try defining a new Object which extends (either explicitly or just in function) the original Object with a new String field for the name of the Object. Then provide methods for getting that name when you need it. That approach has worked fairly well for me.

While you can easily get the name of a variable's class by invoking .getClass().getCanonicalName() or .getClass().getSimpleName() (depending on whether you want the fully qualified name), it is not so easy to get the name of a variable. Java bytecode does not need to preserve the names of local variables (which are represented using push/pop operations on the stack); however, Java bytecode may contain the original names in comments. So, you might try reading the .class files from the .jar files and attempt to extract the variable names from the .class files, assuming that the compiler has included them as comments. As for member variables (ie fields), you can use reflection to get a class's field names.

您始终可以在对象的类中创建名称变量,并使用该变量旁边的其他变量来传递有关此对象的信息。

The replication of the name (symbol) a is what is inconvenient and a bit noisy. In a language with macros, one could define this so that the copying would be automatic in the source code program.

make(A, a);

or makeA(a)

I had the same problem, look that solution...

    DocumentoMB mb = new DocumentoMB();
    System.out.println(mb.getClass().getSimpleName());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM