Java Class New Instance newInstance(Class cls)

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

Description

new Instance

License

Open Source License

Declaration

public static Object newInstance(Class<?> cls) 

Method Source Code

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

import java.lang.reflect.InvocationTargetException;

public class Main {
    public static Object newInstance(Class<?> cls) {
        try {/*from   w ww.j  a  v  a2  s .c om*/
            return cls.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Object newInstance(Class<?> cls, Class<?>[] params, Object... args) {
        try {
            return cls.getConstructor(params).newInstance(args);
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related

  1. newInstance(Class clazz, Class[] args, Object[] objects)
  2. newInstance(Class clazz, Object... args)
  3. newInstance(Class clazz, Object... args)
  4. newInstance(Class cls)
  5. newInstance(Class cls)
  6. newInstance(Class cls, Map, Constructor> cache)
  7. newInstance(Class componentType, int... dimensions)
  8. newInstance(Class entity)
  9. newInstance(Class theClass, Class expected)