Java – How to iterate through all properties of a Java bean

javajavabeansreflection

Below is my bean structure. Employee.java is the parent bean. I would like to iterate through all the properties till the Zip.java and manipulate the values.

I tried to iterate this using reflection, but getDeclaredFields() will give the fields of the top level object only. How to iterate over deeper objects.

Could someone let me know how to do this in java.

Employee.java

private String id;
private String name;
private int age;
private Address addr;
private Contact cont;

Address.java

private String addr1;
private String addr2;
private String city;
private Zip zip;

Contact.java

private String phone;
private String email;

Zip.java

private String zipCd;
private String zipExt;

Best Solution

getDeclaredFields()

for (Field field : yourObject.getClass().getDeclaredFields()) {
//do stuff
}