Accessing the value of a variable by its name as string in Java

I suggest that you use a Map<String, Integer> instead:

Create the map by doing

Map<String, Integer> values = new HashMap<String, Integer>();

Then change

int temp = 10;

to

values.put("temp", 10);

and access the value using

int tempVal = values.get(temp_name);

Leave a Comment