Example usage for java.lang.reflect Constructor newInstance

List of usage examples for java.lang.reflect Constructor newInstance

Introduction

In this page you can find the example usage for java.lang.reflect Constructor newInstance.

Prototype

@CallerSensitive
@ForceInline 
public T newInstance(Object... initargs) throws InstantiationException, IllegalAccessException,
        IllegalArgumentException, InvocationTargetException 

Source Link

Document

Uses the constructor represented by this Constructor object to create and initialize a new instance of the constructor's declaring class, with the specified initialization parameters.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Constructor con = Point.class.getConstructor(new Class[] { int.class, int.class });
    Point obj = (Point) con.newInstance(new Object[] { new Integer(1), new Integer(1) });
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Class[] classParm = null;/*from w w  w. jav a2  s  .  c o  m*/
    Object[] objectParm = null;

    try {
        String name = "java.lang.String";
        Class cl = Class.forName(name);
        java.lang.reflect.Constructor co = cl.getConstructor(classParm);
        System.out.println(co.newInstance(objectParm));
    } catch (Exception e) {
        e.printStackTrace();

    }

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class clazz = String.class;

    Constructor constructor = clazz.getConstructor(new Class[] { String.class });

    String object = (String) constructor.newInstance(new Object[] { "Hello World!" });
    System.out.println("String = " + object);

    constructor = clazz.getConstructor(new Class[] { StringBuilder.class });
    object = (String) constructor.newInstance(new Object[] { new StringBuilder("Hello Universe!") });
    System.out.println("String = " + object);
}

From source file:ConstructorDemo.java

/** 
 * Run the demo./*from  ww  w  .  ja  v  a  2  s . c  o  m*/
 *
 * @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();
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    String name = "java.lang.String";
    String methodName = "toLowerCase";

    Class cl = Class.forName(name);
    java.lang.reflect.Constructor constructor = cl.getConstructor(new Class[] { String.class });
    Object invoker = constructor.newInstance(new Object[] { "AAA" });
    Class arguments[] = new Class[] {};
    java.lang.reflect.Method objMethod = cl.getMethod(methodName, arguments);
    Object result = objMethod.invoke(invoker, (Object[]) arguments);
    System.out.println(result);/*from   ww  w  . j  a  v a2s .c om*/
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Constructor con = java.awt.Point.class.getConstructor(new Class[] { int.class, int.class });
    java.awt.Point obj = (java.awt.Point) con.newInstance(new Object[] { new Integer(123), new Integer(123) });
}

From source file:ArrayCreator.java

public static void main(String... args) {
    Matcher m = p.matcher(s);/*from   w ww  .  j  ava2s.c  o  m*/

    if (m.find()) {
        String cName = m.group(1);
        String[] cVals = m.group(2).split("[\\s,]+");
        int n = cVals.length;

        try {
            Class<?> c = Class.forName(cName);
            Object o = Array.newInstance(c, n);
            for (int i = 0; i < n; i++) {
                String v = cVals[i];
                Constructor ctor = c.getConstructor(String.class);
                Object val = ctor.newInstance(v);
                Array.set(o, i, val);
            }

            Object[] oo = (Object[]) o;
            out.format("%s[] = %s%n", cName, Arrays.toString(oo));

            // production code should handle these exceptions more gracefully
        } catch (ClassNotFoundException x) {
            x.printStackTrace();
        } catch (NoSuchMethodException x) {
            x.printStackTrace();
        } catch (IllegalAccessException x) {
            x.printStackTrace();
        } catch (InstantiationException x) {
            x.printStackTrace();
        } catch (InvocationTargetException x) {
            x.printStackTrace();
        }
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Class cls = Class.forName("constructor2");
    Class partypes[] = new Class[2];
    partypes[0] = Integer.TYPE;/* w w w  .  j a v a 2  s .  c o  m*/
    partypes[1] = Integer.TYPE;
    Constructor ct = cls.getConstructor(partypes);
    Object arglist[] = new Object[2];
    arglist[0] = new Integer(37);
    arglist[1] = new Integer(47);
    Object retobj = ct.newInstance(arglist);

}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Class c = Class.forName("MyClass");

    Constructor constructors[] = c.getDeclaredConstructors();
    Object obj = null;/*from   w w w.  j a  v  a 2 s. com*/
    for (Constructor cons : constructors) {
        Class[] params = cons.getParameterTypes();
        if (params.length == 1 && params[0] == int.class) {
            obj = cons.newInstance(10);
            break;
        }
    }

    if (obj == null) {
        System.out.println("Can't Create MyClass object.");
        return;
    }
}

From source file:EmailAliases.java

public static void main(String... args) {
    try {//from w  ww  .ja va2s .  c o  m
        Constructor ctor = EmailAliases.class.getDeclaredConstructor(HashMap.class);
        ctor.setAccessible(true);
        EmailAliases email = (EmailAliases) ctor.newInstance(defaultAliases);
        email.printKeys();

        // production code should handle these exceptions more gracefully
    } catch (InstantiationException x) {
        x.printStackTrace();
    } catch (IllegalAccessException x) {
        x.printStackTrace();
    } catch (InvocationTargetException x) {
        x.printStackTrace();
    } catch (NoSuchMethodException x) {
        x.printStackTrace();
    }
}