0
class A{
   String name;
   B b;
}

class B{
   String text;
}

Now if I do A.class.getField("text"); This will give exception. How can I use reflection to see if field exist in Containing class B

If its a direct field in Class A reflection works. But if the field is within another containing Class Object(In this case class B) it wont retrieve the field

  • Possible duplicate of: https://stackoverflow.com/questions/7324991/see-if-field-exists-in-class – Imesha Sudasingha Mar 04 '21 at 07:24
  • In this the main class contains direct fields. But in my case 'B' is a separate class contained in class 'A' and that class B contains the field 'text'. – Sanketh Shetty Mar 04 '21 at 07:34
  • "if the field is within another containing Class Object(In this case class B) it wont retrieve the field" that is normal, you need to specify from which class you want to get that field and that field should be declared in that class or in its supertype. Otherwise if you have lets say `class A { B b; C c; }` and both `B` and `C` have their own `text` field, which one should `Field` object returned by `getField()` represent? – Pshemo Mar 04 '21 at 07:39
  • 1
    `B` is not “contained in class `A`”. Whether `B` has a field named `text` does not depend on `A`. – Holger Mar 04 '21 at 13:29

0 Answers0