The JavaTM Tutorial

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

Trail: The Reflection API
Lesson: Manipulating Objects

## Using Constructors that Have Arguments

> To create an object with a constructor that has arguments, you invoke the `newInstance` method on a `Constructor` object, not a `Class` object. This technique involves several steps:
> 1. Create a `Class` object for the object you want to create.
> 2. Create a `Constructor` object by invoking `getConstructor` on the `Class` object. The `getConstructor` method has one parameter: an array of `Class` objects that correspond to the constructor's parameters.
> 3. Create the object by invoking `newInstance` on the `Constructor` object. The `newInstance` method has one parameter: an `Object` array whose elements are the argument values being passed to the constructor.

> The sample program that follows creates a `Rectangle` with the constructor that accepts two integers as parameters. Invoking `newInstance` on this constructor is analogous to this statement:

> >     Rectangle rectangle = new Rectangle(12, 34);

> This constructor's arguments are primitive types, but the argument values passed to `newInstance` must be objects. Therefore, each of the primitive `int` types is wrapped in an `Integer` object.

> The sample program hardcodes the argument passed to the `getConstructor` method. In a real-life application such as a debugger, you would probably let the user select the constructor. To verify the user's selection, you could use the methods described in the section Discovering Class Constructors.

> The source code for the sample program follows:

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

> >     class SampleInstance {

> >        public static void main(String[] args) {

> >           Rectangle rectangle;
> >           Class rectangleDefinition;
> >           Class[] intArgsClass = new Class[] {int.class, int.class};
> >           Integer height = new Integer(12);
> >           Integer width = new Integer(34);
> >           Object[] intArgs = new Object[] {height, width};
> >           Constructor intArgsConstructor;

> >           try {
> >             rectangleDefinition = Class.forName("java.awt.Rectangle");
> >             intArgsConstructor =
> >                 rectangleDefinition.getConstructor(intArgsClass);
> >             rectangle =
> >                 (Rectangle) createObject(intArgsConstructor, intArgs);
> >           } catch (ClassNotFoundException e) {
> >               System.out.println(e);
> >           } catch (NoSuchMethodException e) {
> >               System.out.println(e);
> >           }
> >        }

> >        public static Object createObject(Constructor constructor,
> >                                          Object[] arguments) {

> >           System.out.println ("Constructor: " + constructor.toString());
> >           Object object = null;

> >           try {
> >             object = constructor.newInstance(arguments);
> >             System.out.println ("Object: " + object.toString());
> >             return object;
> >           } catch (InstantiationException e) {
> >               System.out.println(e);
> >           } catch (IllegalAccessException e) {
> >               System.out.println(e);
> >           } catch (IllegalArgumentException e) {
> >               System.out.println(e);
> >           } catch (InvocationTargetException e) {
> >               System.out.println(e);
> >           }
> >           return object;
> >        }
> >     }

> The sample program prints a description of the constructor and the object that it creates:

> >     Constructor: public java.awt.Rectangle(int,int)
> >     Object: java.awt.Rectangle[x=0,y=0,width=12,height=34]

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

Copyright 1995-2001 Sun Microsystems, Inc. All rights reserved.