0

Now i wanna write a method like this:

public String getObjName(Object obj)

This method return the name of this object,for example,

Student stu = new Student();
String objname = getObjName(stu);

Then objname will be "stu".

I learn a lot about java reflection,but i still confused with this issue,every tips will help.

Ilya
  • 29,135
  • 19
  • 110
  • 158
Umaydie
  • 31
  • 4
  • 1
    I think, this is impossible with reflection API – Ilya Sep 25 '13 at 08:00
  • 1
    Why do you want that? This might be possible with reflection, but certainly it would be too complex to be worth. Alternative is to use a `Map` to store mapping from object name to object, or object to object name. But you should be clear that you really need this. – Rohit Jain Sep 25 '13 at 08:00
  • 1
    possible duplicate of [Java Reflection: How to get the name of a variable?](http://stackoverflow.com/questions/744226/java-reflection-how-to-get-the-name-of-a-variable) – Ilya Sep 25 '13 at 08:01
  • Unclear to me, as to what the requirement is :( Though If I understood this a bit slightly, then I guess you are looking to `return` the name of the object. You can have a variable in the class, that will hold this name for each object (like String objName which will be set everytime you create an object like `Student stud1 = new Student("stud1")), and now override `toString()` method from `Object` class, which will return this `objName` variable of the class. – nIcE cOw Sep 25 '13 at 08:08

4 Answers4

1

You can't obtain variable name using reflection. You can obtain only class name:

stu.getClass().getName();
Sergey Morozov
  • 4,528
  • 3
  • 25
  • 39
  • 1
    How is this answering the question? It's like someone asking you for *Apple*, and you give him *Hammer*, saying you don't have apple. – Rohit Jain Sep 25 '13 at 08:03
  • He is asking for the name of the object he kept for that class..... basically name of the object passed as value to the function. – AurA Sep 25 '13 at 08:06
  • I write that it is impossible. – Sergey Morozov Sep 25 '13 at 08:06
  • I also think the same.... you can get the name of class it is instance of but getting the name of object he kept is not possible. – AurA Sep 25 '13 at 08:07
1

You can use this class:

public final class NamesCollector 
{
   final static Map<String, Object> ALL_NAMES = new WeakHashMap<String, Object>();

   private NamesCollector(){}

   public static <T> T createObject(Class<T> clazz, String name) 
   {
      final T retVal;
      try
      {
         retVal = clazz.newInstance();
      }
      catch (final Exception ex)
      {
         throw new RuntimeException(ex);
      }
      ALL_NAMES.put(name, retVal);
      return retVal;
   }

   public static String getObjName(Object obj)   
   {
      for (Entry<String, Object> entry : ALL_NAMES.entrySet()) 
      {
         if (entry.getValue().equals(obj)) 
         {
             return entry.getKey();
         }
      }
      return null;
   }
} 

Usage:

Student stu =  NamesCollector.createObject(Student.class, "stu");
String name = NamesCollector.getObjName(stu);
Ilya
  • 29,135
  • 19
  • 110
  • 158
1

You cannot access the name of a local variable at runtime. Apart from that its rather useless as the following example shows:

Student stu = new Student();
Student stu2 = stu;

getObjName(stu); // should return "stu"
getObjName(stu2); // should return "stu2"

Two different 'object names' for the same object? Better use .toString()!

isnot2bad
  • 24,105
  • 2
  • 29
  • 50
1

First of all I think this is impossible. Reason for that is that you won't get name of object but of a reference to it. Look at example:

    Student stu = new Student();
    Student stu2 = stu;
    String objname = getObjName(stu);

Which name you think It'll write? You can get name of the class of the instance, but name of the instance reference variable, I don't think so.

Additionally variable reference representation is not always the same in bytecode as you see it in your code.

Matej
  • 7,728
  • 4
  • 23
  • 30