How to get variable name? - java

java

Solution

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

Problem

is possible get variable name? For example: ``` String nameOfCar = "audi"; ``` Now print the result: ``` System.out.println(nameOfCar.getVname???or something similar); ``` Screen: ``` nameOfCar ```

Original source

Related problems