Reflection defines an interface
java.lang.reflect.Member
which is implemented by
java.lang.reflect.Field
,
java.lang.reflect.Method
, and
java.lang.reflect.Constructor
. These objects will be discussed in this lesson. For each member, the lesson will describe the associated APIs to retrieve declaration and type information, any operations unique to the member (for example, setting the value of a field or invoking a method), and commonly encountered errors. Each concept will be illustrated with code samples and related output which approximate some expected reflection uses.
java.lang.reflect.Member
.
Fields have a type and a value. The
java.lang.reflect.Field
class provides methods for accessing type information and setting and getting values of a field on a given object.
public
or transient
Methods have return values, parameters, and may throw exceptions. The
java.lang.reflect.Method
class provides methods for obtained the type information for the parameters and return value. It may also be used to invoke methods on a given object.
The Reflection APIs for constructors are defined in
java.lang.reflect.Constructor
and are similar to those for methods, with two major exceptions: first, constructors have no return values; second, the invocation of a constructor creates a new instance of an object for a given class.