I get names of various fields in a class like this :
Field[] f = MyClass.class.getDeclaredFields(); Sring str = f[0].toString(); MyClass cl = new MyClass();
Now I want to access the (public) field str from the object cl dynamically. How do I do that?
str
cl
Should go like this:
Field[] f = MyClass.class.getDeclaredFields(); MyClass targetObject = new MyClass(); ... Object fieldValue = f[interestingIndex].get(cl);
Mind the exceptions.