What is Reflection? - getConstructors(), getFields(), and getMethods()

By: Lakshmi Viewed: 150 times  Printer Friendly Format    


Reflection is the ability of software to analyze itself. This is provided by the java.lang.reflect package and elements in Class. Reflection is an important capability, needed when using components called Java Beans. It allows you to analyze a software component and describe its capabilities dynamically, at run time rather than at compile time. For example, by using reflection, you can determine what methods, constructors, and fields a class supports.

The package java.lang.reflect has one interface, called Member, which defines methods that allow you to get information about a field, constructor, or method of a class. There are also seven classes in this package.

The following application illustrates a simple use of the Java reflection capabilities. It prints the constructors, fields, and methods of the class java.awt.Dimension. The program begins by using the forName( ) method of Class to get a class object for java.awt.Dimension. Once this is obtained, getConstructors(), getFields(), and getMethods() are used to analyze this class object. They return arrays of Constructor, Field, and Method objects that provide the information about the object. The Constructor, Field, and Method classes define several methods that can be used to obtain information about an object. You will want to explore these on your own. However, each supports the toString( ) method. Therefore, using Constructor, Field, and Method objects as arguments to the println( ) method is straightforward, as shown in the program.

// Demonstrate reflection.
import java.lang.reflect.*;
public class ReflectionDemo1 {
public static void main(String args[]) {
try {
Class c = Class.forName("java.awt.Dimension");
System.out.println("Constructors:");
Constructor constructors[] = c.getConstructors();
for(int i = 0; i < constructors.length; i++) {
System.out.println(" " + constructors[i]);
}
System.out.println("Fields:");
Field fields[] = c.getFields();
for(int i = 0; i < fields.length; i++) {
System.out.println(" " + fields[i]);
}
System.out.println("Methods:");
Method methods[] = c.getMethods();
for(int i = 0; i < methods.length; i++) {
System.out.println(" " + methods[i]);
}
}
catch(Exception e) {
System.out.println("Exception: " + e);
}
}
}

Here is the output from this program:

Constructors:
public java.awt.Dimension()
public java.awt.Dimension(int, int)
public java.awt.Dimension(java.awt.Dimension)
Fields:
public static final long java.io.Serializable.serialVersionUID
public int java.awt.Dimension.width
public int java.awt.Dimension.height
Methods:
public java.lang.Object java.awt.geom.Dimension2D.clone( )
public boolean java.awt.Dimension.equals(java.lang.Object)
public final native java.lang.Class java.lang.Object.getClass( )
public native int java.lang.Object.hashCode( )
public final native void java.lang.Object.notify( )
public final native void java.lang.Object.notifyAll( )
public java.lang.String java.awt.Dimension.toString( )
public final void java.lang.Object.wait( )
throws java.lang.InterruptedException
public final native void java.lang.Object.wait(long)
throws java.lang.InterruptedException
public final native void java.lang.Object.wait(long, int)
throws java.lang.InterruptedException
public double java.awt.Dimension getHeight( )
public double java.awt.Dimension getWidth( )
public void java.awt.Dimension.setSize(double, double)
public void java.awt.geom.Dimension2D.setSize
(java.awt.geom.Dimension2D)
public java.awt.Dimension java.awt.Dimension.getSize( )
public void java.awt.Dimension.setSize(int, int)
public void java.awt.Dimension.setSize(java.awt.Dimension)

This tutorial is an extract from the "The Complete Reference Part 2 by Herbert Schildt".



Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial