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 ...

Get Special Edition Using Java 2 Standard Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.