Java Reference
In-Depth Information
example, if Cell objects are only intended for use within SingleLinkQueue
objects, then it would be reasonable for Cell to be a static nested class
within SingleLinkQueue :
class SingleLinkQueue<E> {
static class Cell<E> {
private Cell<E> next;
private E element;
public Cell(E element) {
this.element = element;
}
public Cell(E element, Cell<E> next) {
this.element = element;
this.next = next;
}
public E getElement() {
return element;
}
/* ... rest of Cell methods as before ... */
}
protected Cell<E> head;
protected Cell<E> tail;
/* ... rest of SingleLinkQueue methods as before ... */
}
The code in Cell and SingleLinkQueue is unchanged except that Cell is de-
clared as a static nested class within SingleLinkQueue . The type variable E
is used for both classes because the element type of a cell must always
match the element type of the queue it is part of, but they are distinct
names. The two different E types are associated in the declarations of
head and tail , where the E type parameter of the SingleLinkQueue class
defines the parameterized type Cell<E> . As always, you should choose
names carefully and avoid confusion. If the type variables should always
 
Search WWH ::




Custom Search