Java Class New Instance newInstance(Class clazz)

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

Description

new Instance

License

Open Source License

Declaration

static public Object newInstance(Class clazz)
            throws IllegalArgumentException, InstantiationException,
            IllegalAccessException, InvocationTargetException,
            SecurityException, NoSuchMethodException 

Method Source Code

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

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class Main {
    static public Object newInstance(Class clazz)
            throws IllegalArgumentException, InstantiationException,
            IllegalAccessException, InvocationTargetException,
            SecurityException, NoSuchMethodException {

        try {/*from   w w  w  . j ava2 s  .c om*/
            return clazz.newInstance();
        } catch (Exception ex) {
            @SuppressWarnings({ "rawtypes", "unchecked" })
            Constructor constructor = clazz.getDeclaredConstructor(null);
            constructor.setAccessible(true);
            return constructor.newInstance(new Object[0]);
        }

    }
}

Related

  1. newInstance()
  2. newInstance(Class clazz)
  3. newInstance(Class clazz)
  4. newInstance(Class clazz)
  5. newInstance(Class clazzName)
  6. newInstance(Class cls)
  7. newInstance(Class cls)
  8. newInstance(Class cmdClass)