Java Reflection: Introduction
Search
Google
Web
cognitus.net

Introduction

The Reflection API is an integral part of the Java Programming Language that provides reification, the representation in a concrete and well known format, of primitive types, classes, and their component parts, which are accessible to the currently active Java virtual machine. Through the use of these representations, a programmer can instantiate new objects and inspect and manipulate the state of existing objects in the JVM even if the object type is unknown until runtime.

While the following is not intended to be a complete listing of the Java Reflection API and it’s provided definitions, the components that this tutorial will attempt to demonstrate are:

  • java.lang.Class<T>

    Class objects represent the primitive types defined by Java Language Specification and instances of the classes in a running Java application.

  • java.lang.reflect.Array – Array is a provider class that supplies static methods which facilitate the dynamic creation and access to Java array type objects.

  • java.lang.reflect.Field – Field objects provide information about and access to a single static or instance field declared in a class.

  • java.lang.reflect.Constructor<T> – Constructor objects provide information about and access to a single constructor declared in a class and the means by which an instance of the declaring class may be created.

  • java.lang.reflect.Method – Method objects provide information about and access to a single static or instance method declared in a class.

The following excerpt is from the Sun Java Tutorial:

"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."



©2003-2006 Michael M. Lampkin

All rights reserved.

Use of this website signifies your agreement to the Terms of Use.