File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why does this not compile? 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 "why does this not compile? " Watch "why does this not compile? " New topic

why does this not compile?

Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521

am i right that the variable T cant be used at runtime? that is why i get a compile error here?

scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later" someone unknown
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
I guess have only 2 days left for exam dont confuse yourself, you can explore things after the exam to get confused.

SCJP 1.6 96%
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
this will help you a little bit.
PrasannaKumar Sathiyanantham
Ranch Hand

Joined: Nov 12, 2009
Posts: 110
I think that this has to do with type erasure...
I don't know about type erasure exactly but will give a simple overview.

please google about type erasure to understand it better( but after the exam okay)

All the best

To err is human,
To forgive is not company policy
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 255

Hi Raju,

I think, your question is why the first one gives the type erasure related error while the first one does not. The answer is that when you compile the second code ie..

compiler generates the corresponding bite code as follows -

In this case, you can see that all the type information was erased and T was converted to Object.... Now think of the first code mentioned by you.. ie

In this case, you are using instance of operator on T. Java compiler did not give this as an option and declared this an error due to obvious reasons that converting (my instanceof T) to (my instanceof Object) after compilation would have confused many developers as they could have been doing it thinking a type safe operation but it wold have not been.

I hope this would clear all you confusion.

Salil Verma
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
thanks salil....upto the point explaination.
I agree. Here's the link:
subject: why does this not compile?
It's not a secret anymore!