aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics - T place holder Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics - T place holder" Watch "Generics - T place holder" New topic
Author

Generics - T place holder

adam Lui
Ranch Hand

Joined: Sep 03, 2007
Posts: 186
is it legal to declare a <T> as object/ primitive type INSIDE a method, or even as a return type?
e.g.



i want to figure it out but I dont know how to write a testing code for this case.
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Yes, you can declare a local reference variable using a type parameter, so your "T obj1" line is indeed valid syntax. But type parameters are always object types (i.e. class or interface), so obj1 will never be a primitive.

However, "new T()" is not allowed. Type parameters are only for compile-time (i.e. static) type checking, but they are "erased" during compilation and do not exist at runtime. As a result of this type erasure, the program does not know what T is at run-time, so it can't instantiate an object of type T. (This is similar to the other question you posed regarding the ClassCastException with toArray()'s return value.)


SCJP 5.0
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Here's something you can play with:



Yours,
Bu.


all events occur in real time
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics - T place holder