Java Utililty Methods Class New Instance

List of utility methods to do Class New Instance

Description

The list of methods to do Class New Instance are organized into topic(s).

Method

ObjectnewInstance(String className, Object arg1)
new Instance
Class[] argTypes = { arg1.getClass() };
Object[] args = { arg1 };
return newInstance(className, argTypes, args);
ObjectnewInstance(String className, Object... args)
new Instance
return newInstance(Class.forName(className), args);
ObjectnewInstance(String className, Object... args)
new Instance
Class<?> newoneClass = Class.forName(className);
return newInstance(newoneClass, args);
ObjectnewInstance(String className, Object... parameters)
new Instance
try {
    Class<?> objClass = Class.forName(className);
    Constructor<?> constructor = getConstructor(objClass, parameters);
    if (constructor != null) {
        return constructor.newInstance(parameters);
} catch (Exception e) {
return null;
ObjectnewInstance(String className, Object... params)
Creates a new instance.
Constructor<?> best = null;
int bestMatch = 0;
for (Constructor<?> c : Class.forName(className).getConstructors()) {
    int p = match(c.getParameterTypes(), params);
    if (p > bestMatch) {
        bestMatch = p;
        best = c;
if (best == null) {
    throw new NoSuchMethodException(className);
return best.newInstance(params);
ObjectnewInstance(String className, Object[] args)
new Instance
Class newoneClass = Class.forName(className);
Class[] argsClass = new Class[args.length];
for (int i = 0, j = args.length; i < j; i++) {
    argsClass[i] = args[i].getClass();
Constructor cons = newoneClass.getConstructor(argsClass);
return cons.newInstance(args);
ObjectnewInstance(String className, Object[] args)
new Instance
Class newoneClass = Class.forName(className);
Class[] argsClass = new Class[args.length];
for (int i = 0, j = args.length; i < j; i++) {
    argsClass[i] = args[i].getClass();
Constructor cons = newoneClass.getConstructor(argsClass);
return cons.newInstance(args);
ObjectnewInstance(String className, Object[] args)
new Instance
try {
    Class newoneClass = Class.forName(className);
    Class[] argsClass = new Class[args.length];
    for (int i = 0, j = args.length; i < j; i++) {
        argsClass[i] = args[i].getClass();
    Constructor cons = newoneClass.getConstructor(argsClass);
    return cons.newInstance(args);
...
ObjectnewInstance(String classname, Properties properties)
Get a new instance of an object based on its classpath.
Object result = null;
Class<?> theClass = null;
try {
    theClass = Class.forName(classname);
    final Constructor constructor = theClass.getConstructor(Class.forName("java.util.Properties"));
    result = constructor.newInstance(properties);
} catch (NoSuchMethodException e) {
    result = newInstance(theClass);
...
ObjectnewInstance(String clazz)
new Instance
checkState(!Strings.isNullOrEmpty(clazz));
try {
    return Class.forName(clazz).newInstance();
} catch (Exception exception) {
    throw new RuntimeException(exception);