This works, but your class is not cleanly defined.
Fields should be named using camelCase notation starting with a lower char:
class Info
{
public String name1;
private String name2;
}
Now you have an object info:
Info info;
Then you want to get the value of name1:
Here is a full Test case showing all:
public class InfoTest extends TestCase{
public static class Info {
private String name1;
public String name2;
protected String name3;
String name4;
/**
* Default constructor.
*/
public Info() {
name1 = "name1Value";
name2 = "name2Value";
name3 = "name3Value";
name4 = "name4Value";
}
}
public void testReflection() throws IllegalArgumentException, IllegalAccessException {
Info info1 = new Info();
Field[] infoFields = info1.getClass().getDeclaredFields();
for (int i = 0; i < infoFields.length; i++) {
Field fieldName = infoFields[i];
System.out.println("the name of the field " + i + ":" + fieldName.getName());
fieldName.setAccessible(true);
Object info1ValObj = infoFields[0].get(info1);
System.out.println("the value of the field: " + info1ValObj.toString());
}
}
}
The output then is:
the name of the field 0:name1
the value of the field: name1Value
the name of the field 1:name2
the value of the field: name1Value
the name of the field 2:name3
the value of the field: name1Value
the name of the field 3:name4
the value of the field: name1Value