mercredi 24 juillet 2019

Reflection : Get fieldName and value

I have class object inside another class.

class A {
  private int id;
}

class B {
private String name;
private A classA;
}

For simple class, I am able to get the fields value and name as below :

MyClass ch = new MyClass();

Class<?> clazz = ch.getClass();

           for(Field field : clazz.getDeclaredFields()) {
               field.setAccessible(true);
               System.out.println(field.getName() + " : " + field.get(ch));

       }

How can I use reflection to get the field name and value when a class is present as class property?





Aucun commentaire:

Enregistrer un commentaire