Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get variable name? - java [duplicate]

Tags:

java

is possible get variable name?

For example:

String nameOfCar = "audi";

Now print the result:

System.out.println(nameOfCar.getVname???or something similar);

Screen:

nameOfCar
like image 739
user3784463 Avatar asked Jul 12 '14 06:07

user3784463


1 Answers

You can get all field name by reflection

Class yourClass = YourClass.class
Field[] fields = yourClass.getFields();
for(Field f: fields){
    f.getName();
}

or if you want mapping then go for Map

Map<String, String> propertyToValueMap

if you are trying to read method's local variable name, then it is not that simple to fetch also a signal that you are doing something wrong

like image 114
jmj Avatar answered Oct 14 '22 23:10

jmj