Java Class New Instance newInstance(String className, Object... args)

Here you can find the source of newInstance(String className, Object... args)

Description

new Instance

License

Open Source License

Declaration

public static Object newInstance(String className, Object... args) throws Exception 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.lang.reflect.Constructor;

public class Main {

    public static Object newInstance(String className, Object... args) throws Exception {
        Class<?> newoneClass = Class.forName(className);
        return newInstance(newoneClass, args);
    }/*  ww w.  j  av  a 2 s  . c  om*/

    public static Object newInstance(Class<?> clazz, Object... args) throws Exception {
        Class<?>[] argsClass;
        if (args == null) {
            argsClass = new Class[0];
        } else {
            argsClass = new Class[args.length];
            for (int i = 0, j = args.length; i < j; i++) {
                argsClass[i] = args[i].getClass();
            }
        }
        Constructor<?> cons = clazz.getConstructor(argsClass);
        return cons.newInstance(args);
    }
}

Related

  1. newInstance(String className, Class context)
  2. newInstance(String className, Class castTo)
  3. newInstance(String className, Class[] parmsCls, Object[] parms)
  4. newInstance(String className, Object arg1)
  5. newInstance(String className, Object... args)
  6. newInstance(String className, Object... parameters)
  7. newInstance(String className, Object... params)
  8. newInstance(String className, Object[] args)
  9. newInstance(String className, Object[] args)