|
|
Start of Tutorial > Start of Trail |
Search
Feedback Form |
If you are writing a class browser, you need a way to get information about classes at runtime. For example, you might want to display the names of the class fields, methods, and constructors. Or, you might want to show which interfaces are implemented by a class. To get this information you need to get theClassobject that reflects the class.
For each class, the Java Runtime Environment (JRE) maintains an immutable
Classobject that contains information about the class. AClassobject represents, or reflects, the class. With the reflection API, you can invoke methods on aClassobject which returnConstructor,
Method, and
Fieldobjects. You can use these objects to get information about the corresponding constructors, methods, and fields defined in the class.
Classobjects also represent interfaces. You invokeClassmethods to find out about an interface's modifiers, methods, and public constants. Not all of theClassmethods are appropriate when aClassobject reflects an interface. For example, it doesn't make sense to invokegetConstructorswhen theClassobject represents an interface. The section Examining Interfaces explains whichClassmethods you may use to get information about interfaces.
First things first.
Before you can find out anything about a class, you must first retrieve its
corresponding Class object.
It's easy to find out the name of aClassobject. All you have to do is invoke thegetNamemethod.
This section shows you the methods you need to call to find out what modifiers a particular class has.
In this section you'll learn how to retrieve all of the
Class objects for the ancestors of a given class.
If you want to find out what interfaces a class implements, then check out this section.
In this section you'll learn how to tell if
a Class object represents an interface or a class.
You'll also get some tips
on how to get more information about an interface.
This section shows you how to
discover what fields belong to a class,
and how to find out more about these fields
by accessing Field objects.
This section, which introduces the Constructor class,
explains how to get information about a class's contructors.
To find out about a class's methods, you need to retrieve
the corresponding Method objects.
This section shows you how to do this.
|
|
Start of Tutorial > Start of Trail |
Search
Feedback Form |
Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.