Java Class New Instance newInstanceFromClassName(String className, Class classType)

Here you can find the source of newInstanceFromClassName(String className, Class classType)

Description

Creates new instance of class

License

Open Source License

Parameter

Parameter Description
className name of class
classType type of class

Exception

Parameter Description
ClassNotFoundException an exception
InstantiationException an exception
IllegalAccessException an exception

Return

new instance of class

Declaration

public static <T> T newInstanceFromClassName(String className, Class<T> classType)
        throws ClassNotFoundException, InstantiationException, IllegalAccessException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//  www .  jav a 2 s  .  co  m
     * Creates new instance of class
     *
     * @param className name of class
     * @param classType type of class
     * @return new instance of class
     * @throws ClassNotFoundException
     * @throws InstantiationException
     * @throws IllegalAccessException
     */
    public static <T> T newInstanceFromClassName(String className, Class<T> classType)
            throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        @SuppressWarnings("unchecked")
        final Class<T> clazz = (Class<T>) Class.forName(className).asSubclass(classType);
        return clazz.newInstance();
    }
}

Related

  1. newInstanceByName(String className)
  2. newInstanceByName(String className)
  3. newInstanceForClass(Class type)
  4. newInstanceForName(String fullClassName, Object... args)
  5. newInstanceFromClass(final Class clazz)
  6. newInstanceFromUnknownArgumentTypes(Class cls, Object[] args)
  7. newInstanceHard(String name)
  8. newInstanceOf(Class clazz)
  9. newInstanceOf(String className)