Retrieve a static variable using its name dynamically using reflection
Posted
by
user2538438
on Stack Overflow
See other posts from Stack Overflow
or by user2538438
Published on 2013-07-01T09:37:50Z
Indexed on
2013/07/01
10:21 UTC
Read the original article
Hit count: 188
java
|reflection
How to retrieve a static variable using its name dynamically using Java reflection?
If I have class containing some variables:
public class myClass {
string [][] cfg1= {{"01"},{"02"},{"81"},{"82"}};
string [][]cfg2= {{"c01"},{"c02"},{"c81"},{"c82"}};
string [][] cfg3= {{"d01"},{"d02"},{"d81"}{"d82"}};
int cfg11 = 5;
int cfg22 = 10;
int cfg33 = 15;
}
And in another class I want variable name is input from user:
class test {
Scanner in = new Scanner(System.in);
String userInput = in.nextLine();
// get variable from class myClass that has the same name as userInput
System.out.println("variable name " + // correct variable from class)
}
Using reflection. Any help please?
© Stack Overflow or respective owner