// generics/InstantiateGenericType.java // (c)2016 MindView LLC: see Copyright.txt // We make no guarantees that this code is fit for any purpose. // Visit http://mindviewinc.com/Books/OnJava/ for more book information. 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); System.out.println( "ClassAsFactory succeeded"); try { ClassAsFactory fi = new ClassAsFactory<>(Integer.class); } catch(Exception e) { System.out.println("ClassAsFactory failed"); } } } /* Output: ClassAsFactory succeeded ClassAsFactory failed */