Chapter 9. Reflection
The Reflection API allows a Java program to inspect and
manipulate itself; it comprises the java.lang.Class
class and the java.lang.reflect
package, which represents
the members of a class with Method
,
Constructor
, and Field
objects.
Reflection can obtain information about a class and its
members. This is the technique that the JavaBeans™ (see Chapter 15) introspection mechanism uses
to determine the properties, events, and methods that are supported by a
bean, for example. Reflection can also manipulate objects in Java. You
can use the Field
class to query and
set the values of fields, the Method
class to invoke methods, and the Constructor
class to create new objects. The
examples in this chapter demonstrate both object inspection and
manipulation using the Reflection API.
In addition to the examples in this chapter, the Java Reflection API also features prominently in Examples Example 15-10 and Example 18-3.
Get Java Examples in a Nutshell, 3rd 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.