Reflection: A New Way to Discover Information about Java classes
By Dan Becker2003-05-24
Using the Reflection Features of Java 1.1: Modifiers
The first bit of information you can obtain from the additional reflection features is the scope and visibility of a given class. To do this, you have to import the new java.lang.reflect package with an import statement at the beginning of the program. You also use the new getModifiers() method of the Class class. This adds one line to the middle of the printType method, right before you print the class name. You are using a static method from the new Modifier class to convert the modifiers of a class to a String that can be printed by a program.
|
This modification results in the output shown below. Notice the classes and interfaces now list their visibility (public) and other modifiers such as synchronized, final, or abstract.
|
Tutorial Pages:
» Introduction
» Outlining the Class Hierarchy Using Java 1.02
» Querying Interfaces Using Java 1.02
» An Improved Java 1.02 Reflection Program
» Using the Reflection Features of Java 1.1: Modifiers
» Using the Reflection Features of Java 1.1: Fields
» Security Issues
» Conclusion
First published by IBM DeveloperWorks
