having some problem with erasure & type inference. I have the following class hierarchy, which doesn't look very complicated:
public class Foo<T> {
}
public class Bar<T> {
private final Class<T> clazz;
public Bar(Class<T> clazz) {
this.clazz = clazz;
}
}
And what I'm trying to do is something like this:
Bar<Foo<?>> bar = new Bar<>(Foo.class);
which, of course, doesn't work, as Foo is not quite Foo<?>
. The question is how to construct such a Bar? I need exactly Bar<Foo<?>>
, not Bar<Foo>
, as there is method which accepts only Bar<Foo<?>>
as a parameter. Appreciate ideas.
You cannot create generic object without providing Type information in <>
, as shown in your question post. You must supply a Type or use the raw version. If you are forced to use the wild card parameter type, then just suppress the warning by using raw type, as shown below
@SuppressWarnings("rawtypes")
Bar<Foo<?>> bar = new Bar(Foo.class);
//Bar is having no type "<>" (but this is not recommended
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments