//: generics/InstantiateGenericType.java // ©2015 MindView LLC: see Copyright.txt import static net.mindview.util.Print.*; class ClassAsFactory { T x; public ClassAsFactory(Class kind) { try { x = kind.newInstance(); } catch(InstantiationException | IllegalAccessException e) { throw new RuntimeException(e); } } } class Employee {} public class InstantiateGenericType { public static void main(String[] args) { ClassAsFactory fe = new ClassAsFactory<>(Employee.class); print("ClassAsFactory succeeded"); try { ClassAsFactory fi = new ClassAsFactory<>(Integer.class); } catch(Exception e) { print("ClassAsFactory failed"); } } } /* Output: ClassAsFactory succeeded ClassAsFactory failed *///:~