Java Class New Instance newInstance(final Class clazz)

Here you can find the source of newInstance(final Class clazz)

Description

Create new instance of the given class or throw RuntimeException if object can't be instantiated.

License

Open Source License

Parameter

Parameter Description
clazz target class
T class type

Return

new object of given class

Declaration

public static <T> T newInstance(final Class<T> clazz) 

Method Source Code

//package com.java2s;

public class Main {
    /**// w  w  w . j  a v a2s.c om
     * Create new instance of the given class or throw RuntimeException if object can't be instantiated.
     * @param clazz target class
     * @param <T> class type
     * @return new object of given class
     */
    public static <T> T newInstance(final Class<T> clazz) {
        try {
            return clazz.newInstance();
        } catch (InstantiationException ex) {
            throw new RuntimeException("Failed to create object of class " + clazz, ex);
        } catch (IllegalAccessException ex) {
            throw new RuntimeException("Failed to create object of class " + clazz, ex);
        }
    }
}

Related

  1. newInstance(Field field)
  2. newInstance(final Class clazz)
  3. newInstance(final Class clazz, final Object[] parameters)
  4. newInstance(final Class arrayClass, final int length)
  5. newInstance(final Class clazz)
  6. newInstance(final Class clazz)
  7. newInstance(final Class clazz)
  8. newInstance(final Class clazz)
  9. newInstance(final Class type, final int length)