I’m having this problem. Shouldn’t the constructor have the same type parameters as the class?
public class MyBuilder<T> {
private final Class<T> clss;
/**
*
* @param clss
*/
public <T> MyBuilder(final Class<T> clss) {
this.clss = (Class<T>) clss; // compiler error here
}
Type mismatch: cannot convert from java.lang.Class<T> to java.lang.Class<T>
If I remove the <T> for the ctor it compiles, but I cannot do :
MyBuilder<Foo> myBuilder = new MyBuilder<Foo>(); // compiler error here
The error is The constructor MyBuilder<Foo>() is undefined.
Remove the type parameter from the constructor, as well as the cast:
The type parameters of a class,
Tin this case, are implicitly declared for any instance members, including constructors. By explicitly declaringTfor the constructor you were actually masking theTdeclared by the class, causing that confusing error.