Java Reference
In-Depth Information
for (Node<E> n = head; n != null; n = n.next)
System.out.print(n.value + " ");
}
}
In summary, inner classes of generic classes have access to the enclosing class's type parameters,
which can be confusing. The misunderstanding illustrated in this puzzle is common among
programmers first learning generics. It isn't necessarily wrong to have an inner class in a generic
class, but the need for this is rare, and you should consider refactoring your code to avoid it. When
you have one generic class nested inside another, give their type parameters different names, even if
the nested class is static. For language designers, perhaps it makes sense to forbid shadowing of
type parameters, in the same way that shadowing of local variables is forbidden. Such a rule would
have caught the bug in this puzzle.
< Day Day Up >
 
 
Search WWH ::




Custom Search