I have a class declared to hold information. Let's say it has these fields
class data {
int a;
int b;
int c;
}
And I want to access these fields like this:
String [] fields = {"a", "b", "c"};
data da = new data();
for (int i = 0; i < 3; i++)
if (da.fields[i] < 10)
dosomething();
Is there any way in Java to do this? Googling, I got some results about something called "reflection, " but I've never really heard of that, and I don't think it's quite what I want. Is there any way to do this in Java? If not, are there any languages that support this kind of thing (just out of curiosity)?
Reflection is probably what you need. But what you need more is a hard look at your design. You should avoid reflection where possible.
If you are still interested in doing this, take a look at Java Reflection: Fields.
Is what you would do to get the field by that name. A more detailed example of what you want.