wood burning stoves 2.0*
The moose likes Java in General and the fly likes Type of Generic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Type of Generic" Watch "Type of Generic" New topic
Author

Type of Generic

Mic Popolin
Greenhorn

Joined: Jun 24, 2002
Posts: 3
Can I get the type of the generic using package java.lang.reflect?
Like:

class Boo<E>{
execute(){
//How can I get the Class of E ???
}
}

or

class TesteBoo(Boo<?> boo){
//What the type of '?' ???
}

Thank's


Popolin.:<br />The Power Of The Dark Side
Geoffrey Falk
Ranch Hand

Joined: Aug 17, 2001
Posts: 171
    
    1
No, you can't. Generics are meaningful only at the source code level. They are not preserved in the class file. So there is no way by reflection to get information about generics from the compiled class.

This is called "erasure." The reason for it is to preserve binary compatibility with older compiled code that does not use generics.

Geoffrey


Sun Certified Programmer for the Java 2 Platform
 
 
subject: Type of Generic