You have two main choices.
Reflection
This way is not statically type safe. That is, the compiler gives you no protection against using types that don't have the necessary constructor.
public class SomeClass< T > {
private final Class< T > clsT;
public SomeClass( Class< T > clsT ) {
this.clsT = clsT;
}
public someMethod() {
T t;
try {
t = clsT.getConstructor( context.getClass() ).newInstance( context );
} catch ( ReflectiveOperationException roe ) {
// stuff that would be better handled at compile time
}
// use t
}
}
Factory
You have to declare or import a Factory< T > interface. There is also an extra burden on the caller to supply an instance thereof to the constructor of SomeClass which further erodes the utility of your class. However, it's statically type safe.
public class SomeClass< T > {
private final Factory< T > fctT;
public SomeClass( Factory< T > fctT ) {
this.fctT = fctT;
}
public someMethod() {
T t = fctT.make( context );
// use t
}
}
CustomView, your class wouldn't care).