The accepted answer to this question describes how to create an instance of T
in the Generic<T>
class. This involves passing in a Class<T>
parameter to the Generic
constructor and callin the newInstance
method from that.
A new instance of Generic<Bar>
is then created, and the parameter Bar.class
is passed in.
What do you do if the generic type parameter for the new Generic
class is not some known class like Bar
but is itself a generic type parameter? Suppose I had some other class Skeet<J>
and I wanted to create a new instance of Generic<J>
from inside that class. Then, if I try to pass in J.class
I get the following compiler error:
cannot select from a type variable.
Is there any way around this?
The specific bit of code triggering the error for me is:
public class InputField<W extends Component & WidgetInterface>
extends InputFieldArray<W>
{
public InputField(String labelText)
{
super(new String[] {labelText}, W.class);
}
/* ... */
}
public class InputFieldArray<W extends Component & WidgetInterface>
extends JPanel
{
/* ... */
public InputFieldArray(String[] labelText, Class<W> clazz)
throws InstantiationException, IllegalAccessException
{
/* ... */
for (int i = 0 ; i < labelText.length ; i++) {
newLabel = new JLabel(labelText[i]);
newWidget = clazz.newInstance();
/* ... */
}
/* ... */
}
/* ... */
}
The error happens, because I can't write W.class
. Is there some other way of passing in the same information?
Skeet<J>
right? You need to pass that class to theGeneric
constructor. If you show actual code, we can help you better. – Rohit JainJ.class
because of type erasure. But you can get concreteClass
variable onSkeet<J>
construction – RiaDT.class
- you can't use.class
on type parameters. He meant aClass<T>
would need to be passed in by the caller. – Paul BelloraJ
. Is that possible? – John Gowers