The JavaTM Tutorial
Previous Page Trail Contents Next Page Start of Tutorial Search

Trail: The Reflection API

by Dale Green


Your feedback is important to us! If you have comments about this trail, send them to: tutorial@java.sun.com. In your message, put reflection in the subject header.
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. 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! 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 it in the classes that perform the needed action.

Other trails use the term "member variable" instead of "field." The two terms are synonymous. Because the Field class is part of the reflection API, this trail uses the term "field."

This trail uses 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 classes and interfaces.

Manipulating Objects shows you how to instantiate classes, get or set field values, and invoke methods. With the reflection API, you can perform these tasks even if the names of the classes, fields, and methods are unknown until runtime.

Working with Arrays describes the APIs used to create and to modify arrays whose names are not known until runtime.

Summary of Classes lists the classes that comprise the reflection API, and provides links to the apppropriate API documentation.
Previous Page Trail Contents Next Page Start of Tutorial Search