I know that in Java generics when using a type parameter with multiple bounds,the compiler erases the type information to "the leftmost bound" (i.e. the first class/enum or interface that's on the list). So why is it that the following code compiles without problems?
public class Generic<T extends Object & Appendable & AutoCloseable> {
T t;
T method() throws Exception {
t.close();
char c='\u0000';
t.append(c);
return t;
}
public <T> T method2(T t) {
return t;
}
}
shouldn't the type parameter T be treated as Object?? (thus disallowing me to call close() or append())??
method
? – Neilet.close()
despite of type erasure? – DociluAutoCloseable
and the OP expects it to be erased toObject
. also see my answer (that is not really an answer yet) – Ferromagnetic