Invoke a constructor in Java

Description

The following code shows how to invoke a constructor.

Example


/*  ww w  . java  2s  .  c om*/
import java.lang.reflect.Constructor;

public class Main {
  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();
    }
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Reflection »




Annotation
Array
Class
Constructor
Field
Generics
Interface
Method
Modifier
Package
Proxy