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


Below is my bean structure. is the parent bean. I would like to iterate through all the properties till the 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.

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

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

private String phone;
private String email;

private String zipCd;
private String zipExt;

Best Solution


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