Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get String's reference name in java at runtime? [duplicate]

Tags:

java

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'?

like image 869
Ruban Avatar asked May 12 '26 14:05

Ruban


1 Answers

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: -

String str = "Rohit";
String abc = str;

Now, you have two references str and abc pointing the same literal Rohit.

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?

like image 77
Rohit Jain Avatar answered May 14 '26 04:05

Rohit Jain