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)
new Instance
try {
    return Class.forName(className).newInstance();
} catch (Exception e) {
    throw new RuntimeException("new instance fail : " + e.getMessage(), e);
ObjectnewInstance(String className)
new Instance
return newInstance(className, new Object[0]);
TnewInstance(String className)
new Instance
return newInstance((Class<T>) Class.forName(className));
ObjectnewInstance(String className)
New instance.
Class<?> clazz;
try {
    clazz = Class.forName(className);
    return newInstance(clazz);
} catch (ClassNotFoundException e) {
    throw new RuntimeException(e);
ObjectnewInstance(String className)
new Instance
Object result = null;
Class<?> type = loadClass(className);
result = (type != null ? type.newInstance() : null);
return result;
TnewInstance(String className, Class instanceClazz)
Returns a new instance of classname and check that it's assignable from expected class
if (className == null) {
    return null;
try {
    Class<?> clazz = Class.forName(className);
    if (instanceClazz.isAssignableFrom(clazz)) {
        return (T) clazz.getConstructor().newInstance();
    } else {
...
ObjectnewInstance(String className, Class cls)
new Instance
Class<?> clazz = Class.forName(className);
Object instance = clazz.newInstance();
if ((instance != null) && !cls.isAssignableFrom(instance.getClass()))
    throw new IllegalArgumentException(className + " is not an instanceof " + cls.getName());
return instance;
TnewInstance(String className, Class context)
Instantiates an object using its default constructor if the className is found in the classpath and loaded.
Class<T> clazz = (Class<T>) loadClass(className, context, false);
if (clazz == null)
    throw new Exception(className + " not found in the classpath.");
return clazz.newInstance();
TnewInstance(String className, Class castTo)
new Instance
return castTo.cast(newInstance(forName(className)));
ObjectnewInstance(String className, Class[] parmsCls, Object[] parms)
Creates new instance of an specified class
Class clazz = Class.forName(className);
if (parmsCls == null) {
    return clazz.newInstance();
} else {
    Constructor constr = clazz.getConstructor(parmsCls);
    return constr.newInstance(parms);