Keating
Keating

Reputation: 3530

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

Upvotes: 12

Views: 38330

Answers (8)

Stephen C
Stephen C

Reputation: 718718

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.

Upvotes: 17

devinvestidor
devinvestidor

Reputation: 101

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

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

Upvotes: -1

user4542733
user4542733

Reputation:

You can always create name variable in the object's class, and use that variable beside others to pass information about this object.

Upvotes: 0

guthrie
guthrie

Reputation: 4609

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)

Upvotes: 0

labratmatt
labratmatt

Reputation: 1841

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

Upvotes: 1

Michael Borgwardt
Michael Borgwardt

Reputation: 346260

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

Upvotes: 6

Michael Aaron Safyan
Michael Aaron Safyan

Reputation: 95479

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 (i.e. fields), you can use reflection to get a class's field names.

Upvotes: 0

Chris
Chris

Reputation: 10435

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.

Upvotes: 6

Related Questions