| Author |
Cast issue (when using generics)
|
Robert Merwe
Greenhorn
Joined: Mar 11, 2010
Posts: 1
|
|
Given the code below, I get the following compiler error: Type mismatch: cannot convert from Object to String
What I don't understand, is why the compiler is unable to resolve T to String in this case (by simply removing <X> (which isn't used), or eg declaring newShape as Shape<Integer> all works fine)
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24057
|
|
Hi Robert,
Welcome to JavaRanch!
That's an interesting observation, and I don't have a ready explanation for it. I suspect the answer is to be found in this section of the language spec, but only with a lot of heavy lifting:
http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#341287
|
[Jess in Action][AskingGoodQuestions]
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
Apparently when you drop generics in the declaration, it drops generics completely. Not just the class' generic types but also the method generic types. If you change the declaration in the easiest thing (Shape<?> newShape = ...) it works again.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
 |
|
|
subject: Cast issue (when using generics)
|
|
|