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

前端 未结 2 1881
鱼传尺愫
鱼传尺愫 2020-11-28 16:41

I have a string containing the variable name. I want to get the value of that variable.

int temp = 10;
String temp_name = \"temp\";

Is it p

2条回答
  •  北海茫月
    2020-11-28 17:20

    Make the variable a member variable and use reflection.

    You cannot get the value by name of a variable unless it's a member variable of a class. Then you can use the java.lang.reflect package to retrieve the value.

提交回复
热议问题