What Is Reflection?

   

According to Sun, Reflection is an API that supports introspection of the classes and objects in the current JVM. Essentially, the key word in this definition is introspection. If you're new to Java, this is probably not a term you are familiar with, especially in a software sense. Using Reflection, you can take an object at runtime, such as an ArrayList instance, examine it, and fully describe it. The information you can obtain about an object includes details about its class and about that unique object itself. As far as its class, you can learn which classes it extends, interfaces it implements, methods it provides, and fields it has. After you know this, you can determine the values held by the fields for the particular instance you are examining. The important part is that you can do this without knowing anything about the object in advance, not even that it's an ArrayList.

Reflection is a powerful tool, but it is not something you need in a typical application. Most of the time, you will have full knowledge at compile time of the classes you are using. This has been true for every example in the preceding chapters of this book. The need for Reflection arises when this knowledge is not available. Typically this is the case when extreme flexibility is required in how an application interacts with objects as they are created. Most of the examples of Reflection that you will see in practice are related to development tools, such as debuggers and GUI layout tools. Here, Reflection is a natural fit for querying an object so that its attributes can be displayed or its properties configured for a specific use. Reflection also plays a key role in supporting other parts of the Java API such as JavaBeans (see Chapter 29, "JavaBeans" ).

   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net