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?
Use the
Field.getmethod like this (for the 0th field):To figure out which index the
strfield has you can doHere’s a full example illustrating it:
Output: