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 c)
Instances a new instance of c .
try {
    Constructor<T> noArgConstructor = c.getDeclaredConstructor();
    noArgConstructor.setAccessible(true);
    return noArgConstructor.newInstance();
} catch (NoSuchMethodException e) {
    throw new IllegalArgumentException(c + " does not provide a no-arg constructor");
} catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
    throw new RuntimeException(e);
...
TnewInstance(Class c)
Instantiate the class
try {
    return c.newInstance();
} catch (IllegalAccessException e) {
    throw new RuntimeException("Could not instantiate class " + c.getName(), e);
} catch (InstantiationException e) {
    throw new RuntimeException("Could not instantiate class " + c.getName()
            + " Does it have a public no-argument constructor?", e);
} catch (NullPointerException e) {
...
TnewInstance(Class c, Class[] argTypes, Object[] args)
new Instance
if (!c.isPrimitive())
    return (T) c.getConstructor(argTypes).newInstance(args);
if (argTypes.length != args.length)
    throw new IllegalArgumentException();
if (argTypes.length > 1)
    throw new NoSuchMethodException("Primitive cannot be constructed with more than one value");
if (argTypes.length == 0) {
    return getNeutralValue(c);
...
TnewInstance(Class cl)
Create a new instance of a class
try {
    return cl.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
    throw new RuntimeException(e);
TnewInstance(Class classOf)
new Instance
try {
    return classOf.newInstance();
} catch (InstantiationException | IllegalAccessException ex) {
    return null;
TnewInstance(Class clazz)
new Instance
Constructor<?>[] constructors = clazz.getDeclaredConstructors();
for (Constructor<?> constructor : constructors) {
    if (!constructor.isAccessible())
        constructor.setAccessible(true);
    Class<?>[] types = constructor.getParameterTypes();
    Object[] params = new Object[types.length];
    for (int i = 0; i < types.length; i++) {
        if (types[i] == byte.class) {
...
TnewInstance(Class clazz)
new Instance
try {
    Constructor<T> cons = clazz.getDeclaredConstructor();
    cons.setAccessible(true);
    return cons.newInstance();
} catch (InvocationTargetException e) {
    throw e.getCause();
TnewInstance(Class clazz)
new Instance
if (primitiveSet.contains(clazz)) {
    return null;
if (clazz.isMemberClass() && !Modifier.isStatic(clazz.getModifiers())) {
    Constructor constructorList[] = clazz.getDeclaredConstructors();
    Constructor defaultConstructor = null;
    for (Constructor con : constructorList) {
        if (con.getParameterTypes().length == 1) {
...
TnewInstance(Class clazz)
new Instance
T result;
try {
    Constructor<T> method;
    method = clazz.getDeclaredConstructor();
    method.setAccessible(true);
    result = method.newInstance();
} catch (Exception e) {
    throw new RuntimeException(e);
...
TnewInstance(Class clazz)
Create new instance.
return newInstance(clazz, EMPTY_CLASSES, EMPTY_ARGS);