Possible Duplicate:
Is it possible to get the declaration name of an object at runtime in java?
Is it possible to get the name of a String reference in Java at runtime?
Ex:
String rep="asd";
Here, can I get the name of the rep variable as ‘rep’?
Reference name is nothing but the name of a variable. It is just a reference pointing to an object.
You cannot get that at runtime. Primarily because, there may be conflict between many references that can possibly be pointing to that String object, or any object for that matter.
Think of it: –
Now, you have two references
strandabcpointing the same literalRohit.So, if you want the reference at runtime. which reference will you get? That’s why it is not allowed in Java. Not even through
Reflection.But if you want, you can maintain all your reference names manually. But that would be a tedious job. But I have one question ->
Why on earth would you need that?