aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics type erasure. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics type erasure." Watch "Generics type erasure." New topic
Author

Generics type erasure.

Jan Osykowski
Ranch Hand

Joined: Jul 15, 2010
Posts: 41
Hey,

In the K&B SCJP book I have read that generic types are only for the compilation type. After it, they are removed and they look like old non generic classes. Here I paste 2 codes for which I would like any of you to explain me (please!!!) how it happens that I can actually get the generic type during the run time.

In the first code, we extend the generic class and then through the subclass we are able to get the generic type of the superclass:



The output is:
class P1
class java.lang.String
class P3


The second code does the same but without extending:



And the output is:


class P1
class java.lang.Integer
class P3


Can anyone help me with understanding this 2 codes?

Cheers,
Jan.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

The methods that you're using are part of reflection. That is a mechanism which examines the code at runtime. So you can query the names of methods of a certain class for example. It also allows you to view the generic information. It is however generally considered a bad programming style (of course there are a lot of exceptions) because most problems can be solved with a good use of OOP and architecture. One of the problems of reflection is that you lose compile-time checks made by the compiler.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Jan Osykowski
Ranch Hand

Joined: Jul 15, 2010
Posts: 41
Thanks for the answer. So does it mean that if I use a reflection (I import some field from reflection package) then the compiler doesn't remove the information about the used parameters in the generic types?

Cheers,
Jan.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Jan Osykowski wrote:So does it mean that if I use a reflection (I import some field from reflection package) then the compiler doesn't remove the information about the used parameters in the generic types?

No. reflection is a runtime mechanism . and type erasure happens at compile time. what wouter meant is getting a genetic type information at runtime is possible through reflection APIs, though it is not general practice. go through the earlier discussion in java ranch, below is the link
http://www.coderanch.com/t/383648/java/java/java-reflection-element-type-List
<edit>
Here I have one doubt, probably I will ask in Java General forum
</edit>
Jan Osykowski
Ranch Hand

Joined: Jul 15, 2010
Posts: 41
Hi,

So as I read in the refered topic, the information about the parameterized generic type is not actually erased but it is still saved in the .class file and that's why a reflection can access it?

What doubt do you have about these examples? If you add a new topic in a different subforum please let me know.

Cheers,
Jan.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics type erasure.