Java Class New Instance newInstance(Type type)

Here you can find the source of newInstance(Type type)

Description

Creates a new instance of the type from his type name.

License

Open Source License

Parameter

Parameter Description
Object a parameter

Return

Instance of class represented by type name, if the type is not instanceable, returns null

Declaration

public static Object newInstance(Type type) 

Method Source Code

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

import java.lang.reflect.Type;

public class Main {
    /**/*from   w  w  w  .j  a  v  a 2  s .  c o m*/
     * Creates a new instance of the type from his type name. 
     * The class is instantiated as if by a new expression with an empty argument list
     * 
     * @param Object
     * @return Instance of class represented by type name, if the type is not instanceable, returns null
     */
    public static Object newInstance(Type type) {
        try {
            return Class.forName(type.toString().substring(0, type.toString().indexOf('@'))).newInstance();
        } catch (Throwable e) {
        }
        return null;
    }
}

Related

  1. newInstance(String s)
  2. newInstance(String type)
  3. newInstance(String type, Class cast)
  4. newInstance(T obj)
  5. newInstance(T obj, Class argType, Object arg)
  6. newInstance(Type type)
  7. newInstance(Type type)
  8. newInstanceByName(String className)
  9. newInstanceByName(String className)