My question is exactly similar to the one here. Just that, I want to do it in Java.
Suppose, I want to crate a list as:
List<a_type> myList = new ArrayList<a_type>();
The problem is that what a_type
would be, is decided on the base of a condition. The two possible types are totally unrelated, having no superclass but Object in common. My problem will be solved if I can store the Type in a variable requiredType
and use it as List<requiredType>
. I am not sure if I'm just being greedy or foolish or ignorant in wanting this, but that's the situation.
Although not important, but the scenario that explains why I need it is explained here.
PS: Please do not mention type-erasure. Kindly consider the situation as it is. I could have very well myself mentioned it in the question. If you think there are design issues, kindly read the situation here and suggest alternatives.