wood burning stoves 2.0*
The moose likes Java in General and the fly likes Generic - Type Erasure removes information (at compile time) required at runtime. How does it work? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Generic - Type Erasure removes information (at compile time) required at runtime. How does it work?" Watch "Generic - Type Erasure removes information (at compile time) required at runtime. How does it work?" New topic
Author

Generic - Type Erasure removes information (at compile time) required at runtime. How does it work?

Kamal Tripathi
Ranch Hand

Joined: Oct 02, 2008
Posts: 86
Consider the follow code for Generic. Because of erasure property I defined the methods differently.



However since the compiler erases the generics these classes will not carry this information forward so does this mean if packaged in a jar I will not be able to call these methods. I know that in real world scenario I am still able to call and get good results but how is "type erasure" effective if this information is deleted during compilation yet when it is required at runtime.


Kamal Tripathi
SCJP 1.4 90%, SCWCD5 94%, Next SCDJWS--> In Naescent stage. Researching abt exam and material itself.
Ogeh Ikem
Ranch Hand

Joined: May 13, 2002
Posts: 180
Kamal Tripathi wrote:how is "type erasure" effective...

Type erasure is effective at allowing us to mix legacy code with generics code, however, due to the design of your code, mixing legacy code with generics code will lead to an 'ambiguity' compiler error.

Kamal Tripathi wrote:...when it is required at runtime

It is not required at runtime. At compile time, the compiler removes all information related to type parameters and type arguments. It is able to do this because this information is not required at runtime. At compile time, the compiler decides which overloaded method will be called.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Generic - Type Erasure removes information (at compile time) required at runtime. How does it work?
 
Similar Threads
How java reflection will handle the genericReturnType List<List<List<...>>>?
Genric classes cannot be a subclass of Throwable
generic confusion
What's the reason ?
Compiling problem with generics.