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

TnewInstance(Class klass, Class[] paramTypes, Object... params)
new Instance
final Constructor<T> constructor;
try {
    constructor = klass.getDeclaredConstructor(paramTypes);
    constructor.setAccessible(true);
    return constructor.newInstance(params);
} catch (NoSuchMethodException e) {
    throw new IllegalStateException(e.getMessage(), e);
} catch (InvocationTargetException e) {
...
TnewInstance(Class klass, Object... args)
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.
try {
    Constructor<T> ctor;
    if (args.length == 0) {
        ctor = klass.getDeclaredConstructor();
    } else {
        Class<?>[] argClses = new Class[args.length];
        for (int i = 0; i < args.length; i++) {
            argClses[i] = args[i].getClass();
...
TnewInstance(Class listener)
new Instance
if (listener == null) {
    return null;
try {
    return listener.newInstance();
} catch (InstantiationException e) {
    throw new RuntimeException(e);
} catch (IllegalAccessException e) {
...
TnewInstance(Class sourceClass)
New instance of the specified class.
try {
    return sourceClass.newInstance();
} catch (InstantiationException e) {
    throw new IllegalStateException(e);
} catch (IllegalAccessException e) {
    throw new IllegalStateException(e);
TnewInstance(Class targetType, String className, ClassLoader loader)
new Instance
T o = null;
try {
    o = (T) targetType.getClassLoader().loadClass(className).newInstance();
} catch (ClassNotFoundException e) {
    o = (T) loader.loadClass(className).newInstance();
return o;
TnewInstance(Class tClass, Object... initargs)
new Instance
try {
    return tClass.getConstructor().newInstance(initargs);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException
        | NoSuchMethodException e) {
    throw new RuntimeException(e);
TnewInstance(Class theClass)
Create an object of the given class using a no-args constructor
try {
    return theClass.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
    throw new RuntimeException("Unable to instantiate " + theClass.getName(), e);
TnewInstance(Class theClass)
Construct a class
try {
    return theClass.newInstance();
} catch (Throwable e) {
    if (theClass != null && Modifier.isAbstract(theClass.getModifiers())) {
        throw new RuntimeException("Problem with class: " + theClass + ", maybe because it is abstract!",
                e);
    throw new RuntimeException("Problem with class: " + theClass, e);
...
TnewInstance(Class theClass)
Create an object for the given class and initialize it from conf
T result;
try {
    Constructor<T> meth = theClass.getDeclaredConstructor(EMPTY_ARRAY);
    meth.setAccessible(true);
    result = meth.newInstance();
} catch (Exception e) {
    throw new RuntimeException(e);
return result;
TnewInstance(Class theClass, Class[] parameterTypes, Object[] initargs)
Create an object of the given class.
if (parameterTypes.length != initargs.length) {
    throw new IllegalArgumentException(
            "Number of constructor parameter types doesn't match number of arguments");
for (int i = 0; i < parameterTypes.length; i++) {
    Class<?> clazz = parameterTypes[i];
    if (!(clazz.isInstance(initargs[i]))) {
        throw new IllegalArgumentException("Object : " + initargs[i] + " is not an instance of " + clazz);
...