Here you can find the source of newInstanceFromClassName(String className, Class
Parameter | Description |
---|---|
className | name of class |
classType | type of class |
Parameter | Description |
---|---|
ClassNotFoundException | an exception |
InstantiationException | an exception |
IllegalAccessException | an exception |
public static <T> T newInstanceFromClassName(String className, Class<T> classType) throws ClassNotFoundException, InstantiationException, IllegalAccessException
//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(); } }