Here you can find the source of newInstanceByName(String className)
Parameter | Description |
---|---|
T | the return type |
className | class name including package name |
@SuppressWarnings("unchecked") public static <T> T newInstanceByName(String className)
//package com.java2s; //License from project: Apache License public class Main { /**//from w w w . ja v a 2s. co m * Get the instance of the given class. * * @param <T> * the return type * @param className * class name including package name * @return created instance */ @SuppressWarnings("unchecked") public static <T> T newInstanceByName(String className) { try { Class<?> loader = Class.forName(className); return (T) loader.newInstance(); } catch (Exception e) { throw new RuntimeException(e); } } }