Jeff said:
Can anyone tell me why the following doesn't work?
class test <T extends ArrayList> {
private T e = new ArrayList();
}
Yep. The only thing the compiler knows about "T" is that it's
assignable *to* a variable of type ArrayList, but not necessarily *from*
an expression of type ArrayList. Here you are trying to assign *from*
an expression of type ArrayList to T, and that's not guaranteed to be
valid. To make that clearer, consider this example:
class MyArrayList extends ArrayList { ... }
test<MyArrayList> t = new test<MyArrayList>();
At this point, the constructor would set t's field 'e' (which is of type
MyArrayList) to point to the result of the expression "new ArrayList()".
That's not possible, because the result of that expression *isn't* a
MyArrayList instance.
Incidentally, 'test' is a poor name for a class, and especially
confusing in short bits of sample code on a newsgroup. Your question
and my answer would be far clearer if the class were called 'Test'
instead.
Looking at the decompiled output from JAD, I see:
I'm not sure what you're doing with JAD, so I can't comment on the
result. It looks you've posted a type erasure of the generic type, but
that isn't sufficient to prove that the original code is correct (since,
after all, the whole point of generics are to be more restrictive than
the type erasure). Now, if the type erasure were invalid, then that
*would* prove that generic code is also invalid; but not vice versa.
--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation