Tuesday, May 18, 2010

Small facts about Generics

Small facts about Generics:

  • Type parameters are considered as non static typed variables; hence you can’t have static variable declaration or usage of typed variable in a static method.

  • Type parameters may hide one of actual already declared java type. Caution should be taken when declaring type variable name.

  • Compiler allows your declared type parameter to extend an already declared final java type, it just gives you a warning, no error, may be because to allow to hint and allow get only and put only operations respectively.

  • You may declare an entirely different typed parameter while creating an object and can work with it. But reflection on that object won’t give you expected actual instance types variables, it will give you the actual typed variables declared in the class declaration.

1 comment:

Sujeet Banerjee said...

Well, AFAIK, the whole point of having generics is to do away with the explicit casting; and thereby removing chances of ClassCastException, and also performance costs (if any) associated with explicit casting.