• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics type erasure.

 
Jan Osykowski
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jan Osykowski
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic