I was going through the Java Generics and found the following limitation :-
- Create an array of that static type. This one is the most annoying, but it makes sense because you’d be creating an array of Objects.
Call
instanceof
. This is not allowed because at runtimeList<Integer>
andList<String>
look the same to Java thanks to type erasure. But the following code is compiled fine.interface Shippable<T> { void ship(T t); } class ShippableAbstractCrate<H> implements Shippable<H> { public void ship(H t) { if(t instanceof Object) { //do something } } }
3.Create a static variable as a generic type parameter. This is not allowed because the type is linked to the instance of the class.
Can you please provide the clarification with examples. I'm asking why all 3 points are limitation in generics ?