This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I have one child class(let us say "c1") which inherits from parent class(let us say "p1"). p1 has one public field as "userID". I have object "objectC1" of class c1. I am using a reflection method to get a field "userID" as below,
objectC1.getClass().getDeclaredField("userID"); It comes back with exception that "java.lang.NoSuchFieldException: userID".
This method works fine for all fields declared in class c1.
Do we have a method to find out what are the parent classes for a particular class? So that I can fire the above method on each one of them and get the Field userID. Or there is any other way? Kindly suggest.
Use "getField" instead of "getDeclaredField"; this will return inherited public fields. If the inherited field is not public, then use getSuperclass() to get the class object for p1, and then use getDeclaredField on it.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus