I am not sure why Java is requiring me to cast the return of the makeInstance method to T? What am I doing wrong? Is there a better way to accomplish this?
public class Scratch {
public <T extends Base> T freshen(T instance) {
//why do I need to cast this to T
return makeInstance(instance.getClass());
}
public <T extends Base> T makeInstance(Class<T> type) {
try {
return type.newInstance();
} catch (InstantiationException ex) {
Logger.getLogger(Scratch.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(Scratch.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
public static final class Base {
}
}