Java Class New Instance newInstance(Class type)

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

Description

new Instance

License

Apache License

Declaration

public static <T> T newInstance(Class<T> type) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.lang.reflect.*;

public class Main {
    public static <T> T newInstance(Class<T> type) {
        return newInstance(type, new Class<?>[0]);
    }/*from w  w  w  . j ava 2  s.com*/

    public static <T> T newInstance(Class<T> type, Class<?>[] parameterTypes, Object... parameterValues) {
        try {
            Constructor<T> constructor = type.getDeclaredConstructor(parameterTypes);
            constructor.setAccessible(true);
            return constructor.newInstance(parameterValues);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. newInstance(Class type)
  2. newInstance(Class type)
  3. newInstance(Class type)
  4. newInstance(Class type)
  5. newInstance(Class type)
  6. newInstance(Class type)
  7. newInstance(Class type)
  8. newInstance(Class type)
  9. newInstance(Class type, Class declarator, Annotation annotation)