Demonstrates use of Constructor objects : Constructor « Reflection « Java Tutorial






/*
 *     file: ConstructorDemo.java
 *  package: oreilly.hcj.reflection
 *
 * This software is granted under the terms of the Common Public License,
 * CPL, which may be found at the following URL:
 * http://www-124.ibm.com/developerworks/oss/CPLv1.0.htm
 *
 * Copyright(c) 2003-2005 by the authors indicated in the @author tags.
 * All Rights are Reserved by the various authors.
 *
########## DO NOT EDIT ABOVE THIS LINE ########## */


import java.lang.reflect.Constructor;

/**  
 * Demonstrates use of Constructor objects.
 *
 * @author <a href=mailto:kraythe@arcor.de>Robert Simmons jr. (kraythe)</a>
 * @version $Revision$
 */
public class ConstructorDemo {
  /** 
   * Run the demo.
   *
   * @param args Command line arguments.
   */
  public static void main(final String[] args) {
    try {
      final Class[] ARG_TYPES = new Class[] { String.class };

      Constructor cst = Integer.class.getConstructor(ARG_TYPES);

      System.out.println(cst.newInstance(new Object[] { "45" }));
    } catch (final Exception ex) {
      ex.printStackTrace();
    }
  }
}

/* ########## End of File ########## */








7.3.Constructor
7.3.1.Get all constructors or by parameters
7.3.2.Demonstrates use of Constructor objects
7.3.3.A program that displays a class synopsis for the named class
7.3.4.Create new instance from Constructor
7.3.5.Modifier.PUBLIC | Modifier.PRIVATE | Modifier.PROTECTED
7.3.6.Load class with Class.forName
7.3.7.Call Private constructor
7.3.8.Get constructor by parameter type
7.3.9.Invoke a constructor which throws Exception
7.3.10.Passing a parameter to the constructor and calling a method dynamically
7.3.11.Getting a Constructor of a Class Object: By obtaining a list of all Constructors object
7.3.12.Getting a Constructor of a Class Object: By obtaining a particular Constructor object.
7.3.13.Get a compatible constructor for the given value type
7.3.14.Has Declared Constructor