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(Class clazz)
Attempts to create and return a new instance of given class by invoking default constructor.
try {
    return clazz.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
    throw new RuntimeException(String
            .format("Failed to create an instance of %s by invoking " + "default constructor.", clazz), e);
TnewInstance(Class clazz)
new Instance
try {
    return (T) clazz.newInstance();
} catch (Exception e) {
    throw new RuntimeException("instance Class: " + clazz.getName() + " with ex: " + e.getMessage(), e);
ObjectnewInstance(Class clazz)
new Instance
try {
    return clazz.getConstructor(new Class[0]).newInstance(new Object[0]);
} catch (RuntimeException e) {
    throw e;
} catch (Exception e) {
    throw new RuntimeException(e);
ObjectnewInstance(Class clazz)
Create a new instance
if (clazz == null)
    throw new IllegalArgumentException("Null clazz");
try {
    return clazz.newInstance();
} catch (Throwable t) {
    throw handleErrors("new", clazz.getName(), null, null, t);
ObjectnewInstance(Class clazz)
new Instance
return clazz.newInstance();
ObjectnewInstance(Class clazz)
new Instance
return newInstance(clazz, EMPTY_PARAMS);
ObjectnewInstance(Class clazz, Class[] args, Object[] objects)
new Instance
try {
    return clazz.getConstructor(args).newInstance(objects);
} catch (Exception ex) {
return null;
TnewInstance(Class clazz, Object... args)
new Instance
try {
    if (args == null || args.length == 0) {
        return (T) clazz.newInstance();
    Class<?>[] params = new Class<?>[args.length];
    for (int i = 0; i < args.length; i++) {
        Object arg = args[i];
        params[i] = arg == null ? null : arg.getClass();
...
ObjectnewInstance(Class clazz, Object... args)
Creates a new instance of the specified class
return newInstance(clazz, getArgTypes(args), args);
ObjectnewInstance(Class cls)
new Instance
try {
    return cls.newInstance();
} catch (Throwable t) {
    try {
        Constructor<?>[] constructors = cls.getConstructors();
        if (constructors != null && constructors.length == 0) {
            throw new RuntimeException("Illegal constructor: " + cls.getName());
        Constructor<?> constructor = constructors[0];
        if (constructor.getParameterTypes().length > 0) {
            for (Constructor<?> c : constructors) {
                if (c.getParameterTypes().length < constructor.getParameterTypes().length) {
                    constructor = c;
                    if (constructor.getParameterTypes().length == 0) {
                        break;
        return constructor.newInstance(new Object[constructor.getParameterTypes().length]);
    } catch (InstantiationException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (InvocationTargetException e) {
        throw new RuntimeException(e.getMessage(), e);