36 lines
850 B
Java
36 lines
850 B
Java
// generics/InstantiateGenericType.java
|
|
// ©2015 MindView LLC: see Copyright.txt
|
|
import static com.mindviewinc.util.Print.*;
|
|
|
|
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);
|
|
print("ClassAsFactory<Employee> succeeded");
|
|
try {
|
|
ClassAsFactory<Integer> fi =
|
|
new ClassAsFactory<>(Integer.class);
|
|
} catch(Exception e) {
|
|
print("ClassAsFactory<Integer> failed");
|
|
}
|
|
}
|
|
}
|
|
/* Output:
|
|
ClassAsFactory<Employee> succeeded
|
|
ClassAsFactory<Integer> failed
|
|
*/
|