Previous | Next | Trail Map | The Reflection API | Contents

The Reflection API

by Dale Green

The Reflection API represents, or reflects, the classes, interfaces, and objects in the current Java Virtual Machine. You'll want to use the Reflection API if you are writing development tools such as debuggers, class browsers, and GUI builders. With the Reflection API you can:

First, a note of caution. Please don't use the Reflection API when other tools more natural to the Java programming language would suffice. For example, if you are in the habit of using function pointers in another language, you might be tempted to use the Method objects of the Reflection API in the same way. Resist the temptation! In this case, your program will be easier to debug and maintain if you don't use Method objects. Instead, you should define an interface, and then implement the interface in the classes that perform the needed action.


Note: The Reflection APIs documented in this trail were added to the Java Development Kit for its 1.1 release. The code examples in this trail will work only on Java platforms that support the JDK software releases 1.1 or 1.2.

This trail employs a task oriented approach to the Reflection API. Each lesson includes a set of related tasks, and every task is explained, step-by-step, with a sample program. The lessons are as follows:

Examining Classes explains how to determine the class of an object, and how to get information about the class.

Manipulating Objects shows you how to dynamically create objects, get or set field values, and invoke methods.

Working with Arrays describes the APIs used to dynamically create and modify arrays.

Summary of Classes lists the classes that comprise the Reflection API, and provides links to the apppropriate API documentation.

Please send comments about this trail to: tutorial@java.sun.com. In your message, place reflection in the subject header. Your feedback is important to us!


Previous | Next | Trail Map | The Reflection API | Contents