Java Reference
In-Depth Information
The intercession features provided by Java lets you create an instance of a class whose name is not known until
runtime, invoke methods on such instances, and get/set its fields. However, Java does not allow you to change the
data structure at runtime. For example, you cannot add a new field or a method to an object at runtime. All fields
of an object are always determined at compile time. Examples of behavioral intercession are the ability to change
the method execution at runtime or add a new method to a class at runtime. Java does not provide any of these
intercession features. That is, you cannot change a class's method code at runtime to change its execution behavior;
neither can you add a new method to a class at runtime.
Java provides reification by providing an object representation for a class and its methods, constructors, fields,
etc. at runtime. In most cases, Java does not support reification for generic types. Java 5 added support for generic
types. Please refer to Chapter 4 for more details on generic types. A program can work on the reified objects in order
to get information about the runtime execution. For example, you have been using the object of java.lang.Class
class to get the information about the class of an object. A Class object is the reification of the bytecode for the class
of an object. When you want to gather information about the class of an object, you do not have to worry about the
bytecode of the class from which the object was instantiated. Rather, Java provides the reification of the bytecode as an
object of the Class class.
The reflection facility in Java is provided through the reflection API. Most of the reflection API classes and
interfaces are in the java.lang.reflect package. The Class class which is central to the reflection in Java, is in the
java.lang package. Some of the frequently used classes in reflection are listed in Table 3-1 .
Table 3-1. Commonly Used Classes in Reflection
Class Name
Description
java.lang.Class
An object of this class represents a single class loaded by a class loader in
the JVM.
java.lang.reflect.Field
An object of this class represents a single field of a class or an interface.
The field represented by this object may be a static field or an instance field.
java.lang.reflect.Constructor
An object of this class represents a single constructor of a class.
java.lang.reflect.Method
An object of this class represents a method of a class or an interface.
The method represented by this object may be a class method or an instance
method.
java.lang.reflect.Modifier
This class has static methods that are used to decode the access modifiers for
a class and its members.
java.lang.reflect.Array
This class provides static methods that are used to create arrays at runtime.
Some of the things you can do using the reflection features in Java are as follows:
If you have an object reference, you can find out the class name of the object.
If you have a class name, you can know its full description, for example, its package name, its
access modifiers, etc.
If you have a class name, you can find out the methods defined in the class, their return type,
access modifiers, parameters type, parameter names, etc. The support for parameter names
was added in Java 8.
If you have a class name, you can find out all field descriptions of the class.
 
Search WWH ::




Custom Search