Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 352
Keating Avatar asked May 24 '10 00:05

Keating


Video Answer


3 Answers

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.

like image 114
Stephen C Avatar answered Sep 28 '22 18:09

Stephen C


You can use Component.setName() to give names to Swing and AWT components.

like image 45
Michael Borgwardt Avatar answered Sep 28 '22 17:09

Michael Borgwardt


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.

like image 36
Chris Avatar answered Sep 28 '22 18:09

Chris