Accessing the Declared Fields of a Class

With constructors and methods covered, that leaves fields as the major remaining aspect of class Reflection. As you might have guessed, the methods used in this endeavor are getDeclaredFields, getDeclaredField, getFields, and getField. This section focuses on getDeclaredFields and getDeclaredField. The other two forms differ from these only in that they reflect superclasses and superinterfaces, and return only public fields.

In the previous Provider class, there was already a field named attribute, so you can modify Requestor slightly to access it, as shown in Listing 28.11.

Code Listing 28.11. Requestor Application That Gets Fields
 /* * Requestor */ import java.lang.reflect.*; public class Requestor ...

Get Special Edition Using Java 2 Standard Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.