Java Class New Instance newInstanceByName(String className)

Here you can find the source of newInstanceByName(String className)

Description

Get the instance of the given class.

License

Apache License

Parameter

Parameter Description
T the return type
className class name including package name

Return

created instance

Declaration

@SuppressWarnings("unchecked")
public static <T> T newInstanceByName(String className) 

Method Source Code

//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);
        }
    }
}

Related

  1. newInstance(T obj)
  2. newInstance(T obj, Class argType, Object arg)
  3. newInstance(Type type)
  4. newInstance(Type type)
  5. newInstance(Type type)
  6. newInstanceByName(String className)
  7. newInstanceForClass(Class type)
  8. newInstanceForName(String fullClassName, Object... args)
  9. newInstanceFromClass(final Class clazz)