简体   繁体   中英

Is it possible to return the name of an Object in Java?

Suppose you create an object:

class newClass{
    public static void main(String[] args){
        Object o = new Object();
    }
}

Is it possible to return the name as a string, "o", or in anyform, of the given object?

No, this is not possible. If you really want this, you are probably using the wrong design. Note that objects do not have a name. Variables do have a name.

否。该对象不知道外界称之为什么,即它有什么参考。

Is it possible to return the name of an Object in Java?

If you write something like this:

String name = obj.toString();

It will return string representation of object but this is not very human-readable. if you want to do it you need to create custom object and override toString() method:

public class MyObject {

   private String name;


   public String toString() {
      return name;
   }
}

Now when you write:

String name = myObj.toString();

It will return name in human-readable form. But this depends of your requirements if you want to create custom objects. In this way it's very simple and it works well.

This is not possible.

You could use a Map<String, Object> to store a mapping from object name to object value and then return the key.

In reality the "name" of your object is a compile time constant. Ie it doesn't change once you have compiled the code so

Object o = new Object();
return "o";

Would literally do what you want. Which begs the question, why would you want to do that?

This is not possible using java reflection. However, the information is stored in java class file if compiled with debugging information on. You may use bytecode engineering library like ASM to dig that.

Note: I just read sajmon's answer and he has the more elegant way of doing it to a certain extent

First of all the guys' answers are correct

but

if you really need it

I would extend Object class with something like MyObject

 public class MyObject {

 private String objectName;

    public MyObject(){
    }


    public MyObject(String name){
      setObjectName(name);
    }

    public getObjectName(){
     return this.objectName;
    }

    public setObjectName(String name){
     this.objectName = name;
    }
    }

but then each time you create an Object use MyObject instance instead and add your object name as a constructor parameter

MyObject mine = new MyObject("mine");

Having said that, it's still an open question as to why you might need it? :)

Since Java supports aliasing this really isn't sensible or possible. Aliasing means there's no garuantee that an object is referenced by only one variable.

scoping just makes the whole problem even harder, since only references in the current scope would be of any use at that point. and there may be more than one.

it would help if you explained what you're trying to achieve with this code.

In addition to the previous answers, there may be several different variables, with different identifiers, that all reference the same object. A variable may refer to different objects at different times.

If you need a mapping from an object to a String, I suggest a Map<Object,String> .

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