While looking into some code I found out that there is following kind of syntax.
protected <T> T Execute(Class<T> returnType){
T t;
return t;
}
What does this mean?? What if I want to save the outcome in some variable of other class?
While looking into some code I found out that there is following kind of syntax.
protected <T> T Execute(Class<T> returnType){
T t;
return t;
}
What does this mean?? What if I want to save the outcome in some variable of other class?
Type parameter has been added to java.lang.Class to enable one specific use of Class objects as type-safe object factories. Essentially, the addition of lets you instantiate classes in a type-safe manner, like this:
T instance = myClass.newInstance();
You can use newInstance() method.
protected <T> T execute(Class<T> returnType) {
T t = returnType.newInstance();
return t;
}
But you will have to handle
InstantiationException, IllegalAccessException
Although this is a strange, unwanted way to create new objects, AbstractFactory would be a better solution.