Another Academic Question [juglist]
<cebmailbox-trijug-/[email protected]>
| Newsgroups | gmane.org.user-groups.trijug.juglist |
|---|---|
| Message-ID | <[email protected]> |
Again, I realize that the following code is not the best written and has possible has zero practical use, however it was written to discover a feature of Generic Programming.
The question is: How come it compiles and runs with no problems?
To my way of thinking, the static variable gets assigned once during a time when the generic type parameter V was of type J1. How then can it hold objects of type J2?
Maybe 'V' is really known as the common interface I1, but then how comes the last line of the program requires no type casting?
Just seeking more understanding of why the following works AS IS.
-Chris
=================================================
public class Hmm2<V extends Hmm2.I1> {
public static interface I1 {}
static class SC1 {}
static SC1 sc1 = null;
class C1 extends SC1 {
public V value;
}
public Hmm2(V v) {
if (sc1 == null)
sc1 = new C1();
((C1)sc1).value = v;
}
public V getValue() {
return ((C1)sc1).value;
}
static class J1 implements I1 {}
static class J2 implements I1 {}
public static void main(String[] args) {
Hmm2<J1> h1 = new Hmm2<J1>(new J1());
Hmm2<J2> h2 = new Hmm2<J2>(new J2());
J2 j2 = h2.getValue();
}
}
=============================================
_______________________________________________
Juglist mailing list
[email protected]
http://trijug.org/mailman/listinfo/juglist_trijug.org