O'Reilly logo

Special Edition Using Java 2 Standard Edition by Geoff Friesen, Chuck Cavaness, Brian Keeton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating a Class Knowing Only the List of Constructors

After you have retrieved the constructors associated with a class, it is also possible to use them to instantiate an object. Listing 28.2 shows a simple example of how to do this.

Code Listing 28.2. Requestor Creates a Provider Without Knowing the Constructor Details in Advance
 /* * Requestor */ import java.lang.reflect.*; public class Requestor { // Return an array of Provider instances that // includes an instance created with each // constructor declared by the class public static Provider[] constructProvider() { Provider[] prov = null; try { // load the class and retrieve its constructors Class cl = Class.forName("Provider"); Constructor[] con = cl.getDeclaredConstructors(); // allocate ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required