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

@SuppressWarnings("unchecked")
public static <T> T newInstance(Class<?> clazz) 

Method Source Code

//package com.java2s;

public class Main {

    @SuppressWarnings("unchecked")
    public static <T> T newInstance(Class<?> clazz) {
        try {/*from   w  w w . ja  v  a  2  s . c  om*/
            return (T) clazz.newInstance();
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    @SuppressWarnings("unchecked")
    public static <T> T newInstance(String clazz) {
        try {
            return (T) Class.forName(clazz).newInstance();
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. newInstance(Class componentType, int length)
  2. newInstance(Class classZ)
  3. newInstance(Class clazz)
  4. newInstance(Class clazz)
  5. newInstance(Class clazz)
  6. newInstance(Class clazz)
  7. newInstance(Class clazz)
  8. newInstance(Class clazz)
  9. newInstance(Class clazz)