Convert a string to variable name

Hi.. I'm trying to make a forloop to get data for a specific variablename. The variablename changes each time it is run so.. My problem is that I cannot get use the string in the string array as the name of the variable which I like to get the value of. In this case int..

Here you get some of the code: (globals is just a file with global variables.. The names equal to the ones in this file to simplify )

publicstaticint idle = 0;

publicstaticint exit = 0;

String[] cycle ={"idle","exit"};// these is equat to the values defined

public ...{

while(Globals.exit != 1)

{

for (int i=0;i<cycle.length();i++)

{

****** PROBLEM *****if(Globals.(valueOf(cycle[i])) == valueOf(cycle[i]))

// I like to use the the string in cycle as a variable name.. Ex.. int test = idle ( int test = valueOf(cycle[i]) OR HOW IT SHOULD BE.. )

......

Somebody know how to do so..? And sorry for bad formulation, hard to decribe imo.. :S>

[1619 byte] By [sniper83a] at [2007-11-27 8:24:19]
# 1
Look into the method Class.getDeclaredField(). It'll allow you to use a String to get a particular field from your Globals class
georgemca at 2007-7-12 20:13:19 > top of Java-index,Java Essentials,Java Programming...
# 2
Why do you want to do this? You can't declare variable names dynamically and I don't see why you would like to do it. You can use reflection if you want to get the value of known attributes.
kajbja at 2007-7-12 20:13:19 > top of Java-index,Java Essentials,Java Programming...
# 3
Thanks for the very quick reply.. :DI'll look at it right away..
sniper83a at 2007-7-12 20:13:19 > top of Java-index,Java Essentials,Java Programming...
# 4
Instead of writing code for each variable name and compare to an earlier value (globals), I see a loop to do this much better.. This way it is easier to add more values without getting very long code..
sniper83a at 2007-7-12 20:13:19 > top of Java-index,Java Essentials,Java Programming...
# 5
You might want to use a map of some sort.... HashMap<String, Integer> for example...Then you could get Integers by providing Strings...
jadespirita at 2007-7-12 20:13:19 > top of Java-index,Java Essentials,Java Programming...
# 6
Thanks You all.. I could use Hashmaps. Thats right but I don't think that it would be the best solution in this case.. getDeclaredField() was exactly what I was looking for. It works just fine..You have saved my day.. :D
sniper83a at 2007-7-12 20:13:19 > top of Java-index,Java Essentials,Java Programming...