Here you can find the source of newInstance(final Class
Parameter | Description |
---|---|
clazz | target class |
T | class type |
public static <T> T newInstance(final Class<T> clazz)
//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); } } }