aspose file tools
The moose likes Beginning Java and the fly likes Cast issue (when using generics) Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Cast issue (when using generics)" Watch "Cast issue (when using generics)" New topic
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
    
  13

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
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Cast issue (when using generics)
 
Similar Threads
How to Rotate with AffineTransform and keep the orignal coordinates ?
Overriding/overloading.
anonymous class
Drawing text inside a Graphics2D shape
Shapes return 0's as coordinates, width & height