[OCP 21 book] | [OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Java® Language Specification((§4.8) wrote:The type of a constructor (§8.8), instance method (§8.4, §9.4), or non-static field (§8.3) M of a raw type C that is not inherited from its superclasses or superinterfaces is the raw type that corresponds to the erasure of its type in the generic declaration corresponding to C.
Try to enjoy your work while doing it,it will Automatically convert in Hard Work...
Greg Charles wrote:Yes, that's a good way of putting it. Why isn't the compiler more picky in the first example?
Greg Charles wrote:What about the second example causes it to become more picky?
Greg Charles wrote:And, what about the third example makes it not picky again?
Greg Charles wrote:Another interesting point: the unchecked cast warnings you note for the first example are on lines 5 and 7, the casts to T. However, the ClassCastException if it happens, happens on line 15. I can't think of a case where the CCE could actually occur where the warnings say it might.
All things are lawful, but not all things are profitable.
However, the ClassCastException (CCE) if it happens...
All things are lawful, but not all things are profitable.
Try to enjoy your work while doing it,it will Automatically convert in Hard Work...
JLS §4.8 wrote:A non-generic class or interface type is not a raw type.
JLS §4.6 wrote:The type parameters of a constructor or method (§8.4.4), and the return type (§8.4.5) of a method, also undergo erasure if the constructor or method's signature is erased.
Greg Charles wrote:At least, I don't see anything in the JLS (Justice League of Siberia) that says when type erasure is applied to a parametrized class, it must also be extended to unrelated generics used in instance methods
Moreover, I can't see any reason why this would be true.
Greg wrote:...The casts and the compiler warnings are on lines 5 and 7, but the ClassCastException (aka CCE) happens on line 15, where there is no cast...
The compiler works as per the rules described in the java® Language Specification,so it's not a bug it's just obeying the rules.Greg wrote:My contention is this shows there is a bug in the Java compiler.
I think you missed it have a look on it→Generics erasure for conctructor,instance method,non-static field when using a Raw Type..Greg wrote:At least, I don't see anything in the JLS (
Justice League of Siberia) that says when type erasure is applied to a parametrized class, it must also be extended to unrelated generics used in instance methods.
Try to enjoy your work while doing it,it will Automatically convert in Hard Work...
No matter how many women are assigned to the project, a pregnancy takes nine months. Much longer than this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|