Java Reference
In-Depth Information
It is a compile-time error if a generic class is a direct or indirect subclass of Throwable
11.1.1 ) .
This restriction is needed since the catch mechanism of the Java Virtual Machine
works only with non-generic classes.
It is a compile-time error to refer to a type parameter of a generic class C anywhere in:
• the declaration of a static member of C 8.3.1.1 , § 8.4.3.2 , § 8.5.1 ), or
• the declaration of a static member of any type declaration nested within C , or
• a static initializer of C 8.7 ) , or
• a static initializer of any class declaration nested within C .
Generic class declarations can be nested inside other declarations.
Example 8.1.2-2. Nested Generic Classes
Click here to view code image
class Seq<T> {
T head;
Seq<T> tail;
Seq() { this(null, null); }
Seq(T head, Seq<T> tail) {
this.head = head;
this.tail = tail;
}
boolean isEmpty() { return tail == null; }
class Zipper<S> {
Seq<Pair<T,S>> zip(Seq<S> that) {
if (isEmpty() || that.isEmpty()) {
return new Seq<Pair<T,S>>();
} else {
Seq<T>.Zipper<S> tailZipper =
tail.new Zipper<S>();
return new Seq<Pair<T,S>>(
new Pair<T,S>(head, that.head),
tailZipper.zip(that.tail));
}
}
}
}
class Pair<T, S> {
T fst; S snd;
Pair(T f, S s) { fst = f; snd = s; }
Search WWH ::




Custom Search