OnJava8-Examples/generics/InstantiateGenericType.java
2015-12-15 11:47:04 -08:00

38 lines
978 B
Java

// 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> {
T x;
public ClassAsFactory(Class<T> 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<Employee> fe =
new ClassAsFactory<>(Employee.class);
System.out.println(
"ClassAsFactory<Employee> succeeded");
try {
ClassAsFactory<Integer> fi =
new ClassAsFactory<>(Integer.class);
} catch(Exception e) {
System.out.println("ClassAsFactory<Integer> failed");
}
}
}
/* Output:
ClassAsFactory<Employee> succeeded
ClassAsFactory<Integer> failed
*/