The JavaTM Tutorial

| Start of Tutorial > Start of Trail > Start of Lesson | Search

Trail: The Reflection API
Lesson: Examining Classes

## Discovering Class Constructors

> To create an instance of a class, you invoke a special method called a constructor. Like methods, constructors can be overloaded and are distinguished from one another by their signatures.

> You can get information about a class's constructors by invoking the `getConstructors` method, which returns an array of `Constructor` objects. You can use the methods provided by the `Constructor` class to determine the constructor's name, set of modifiers, parameter types, and set of throwable exceptions. You can also create a new instance of the `Constructor` object's class with the `Constructor.newInstance` method. You'll learn how to invoke `Constructor.newInstance` in the section Manipulating Objects.

> The sample program that follows prints out the parameter types for each constructor in the `Rectangle` class. The program performs the following steps:

> 1. It retrieves an array of `Constructor` objects from the `Class` object by calling `getConstructors`.
> 2. For every element in the `Constructor` array, it creates an array of `Class` objects by invoking `getParameterTypes`. The `Class` objects in the array represent the parameters of the constructor.
> 3. The program calls `getName` to fetch the class name for every parameter in the `Class` array created in the preceding step.

> It's not as complicated as it sounds. Here's the source code for the sample program:

> >     import java.lang.reflect.*;
> >     import java.awt.*;

> >     class SampleConstructor {

> >        public static void main(String[] args) {
> >           Rectangle r = new Rectangle();
> >           showConstructors(r);
> >        }

> >        static void showConstructors(Object o) {
> >           Class c = o.getClass();
> >           Constructor[] theConstructors = c.getConstructors();
> >           for (int i = 0; i < theConstructors.length; i++) {
> >              System.out.print("( ");
> >              Class[] parameterTypes =
> >                 theConstructors[i].getParameterTypes();
> >              for (int k = 0; k < parameterTypes.length; k ++) {
> >                 String parameterString = parameterTypes[k].getName();
> >                 System.out.print(parameterString + " ");
> >                 }
> >              System.out.println(")");
> >              }
> >        }
> >     }

> In the first line of output generated by the sample program, no parameter types appear because that particular `Constructor` object represents a no-argument constructor. In subsequent lines, the parameters listed are either `int` types or fully qualified object names. The output of the sample program is:

> >     ( )
> >     ( int int )
> >     ( int int int int )
> >     ( java.awt.Dimension )
> >     ( java.awt.Point )
> >     ( java.awt.Point java.awt.Dimension )
> >     ( java.awt.Rectangle )

| Start of Tutorial > Start of Trail > Start of Lesson | Search