Chapter 16. Dynamic Programming Through Reflection


16. Dynamic Programming Through Reflection

Reflection is a mechanism for discovering data about a program at runtime. Reflection in Java enables you to discover information about fields, methods, and constructors of classes. You can also operate on the fields and methods that you discover. Reflection enables what is commonly referred to as dynamic programming in Java. Reflection in Java is accomplished using the Java Reflection API. This API consists of classes in the java.lang and the java.lang.reflect packages.

The things that you can do with the Java Reflection API include the following:

  • Determine the class of an object

  • Get information about a class's modifiers, fields, methods, constructors, and superclasses

  • Find out what constants and method declarations belong to an interface

  • Create an instance of a class whose name is not known until runtime

  • Get and set the value of an object's field

  • Invoke a method on an object

  • Create a new array, whose size and component type are not known until runtime

The Java reflection API is commonly used to create development tools such as debuggers, class browsers, and GUI builders. Often in these types of tools, you need to interact with classes, objects, methods, and fields, and you do not know which ones at compile time. So the application must dynamically find and access these items.




JavaT Phrasebook. Essential Code and Commands
Java Phrasebook
ISBN: 0672329077
EAN: 2147483647
Year: 2004
Pages: 166

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