There's already too many post here suggesting doing:
class Foo<T>{
Class<T> type;
public Foo(Class<T> type){
this.type = type;
}
// then using newInstance method creating the instance
}
The problem with this approach is that this is not the java that is creating the instance, this is the developer that is forced to pass type into the constructor of the Foo
class. So let's say I need a parameterless constructor, is Java still capable of creating the instance?
For example in C# we got the Activator type which we can use like this:
Object obj = Activator.CreateInstance<T>();
so if I have the below class in Java and I can't follow the first solution, what is an alternative approach that is also similar to the C# solution:
class Foo<T>(){
public Foo(){
if T is of Type X Then Do Y
}
}