Java Reference
In-Depth Information
Chapter 3
Reflection
In this chapter, you will learn
What reflection is
How to use reflection to get information about classes, constructors, methods, etc. at runtime
How to access fields of an object and a class using reflection
How to create objects of a class using reflection
How to invoke methods of a class using reflection
How to create arrays using reflection
What Is Reflection?
Reflection is the ability of a program to query and modify its state “as data” during the execution of the program.
The ability of a program to query or obtain information about itself is known as introspection . The ability of a program
to modify its execution state, modify its own interpretation or its meaning, or add new behaviors to the program as it is
executing is called intercession .
Reflection is further divided into two categories:
Structural reflection
Behavioral reflection
The ability of a program to query about the implementation of its data and code is called structural introspection,
whereas its ability to modify or create new data structure and code is called structural intercession.
The ability of a program to obtain information about its runtime environment is called behavioral introspection,
whereas its ability to modify the runtime environment is called behavioral intercession.
Providing the ability to a program to query or modify its state requires a mechanism for encoding the execution
state as data. In other words, the program should be able to represent its execution state as data elements (as objects
in objected-oriented languages such as Java) so that it can be queried and modified. The process of encoding the
execution state into data is called reification . A programming language is called reflective if it provides the programs
with reflection capability.
Reflection in Java
The support for reflection in Java is mostly limited to introspection. It supports intercession in a very limited form.
The introspection features provided by Java let you obtain class information about an object at runtime. They also let
you obtain information about the fields, methods, modifiers, and the superclass of a class at runtime.
 
Search WWH ::




Custom Search