Java Reference
In-Depth Information
this.element = element;
}
public Cell(E element, Cell<E> next) {
this.element = element;
this.next = next;
}
public E getElement() {
return element;
}
public void setElement(E element) {
this.element = element;
}
public Cell<E> getNext() {
return next;
}
public void setNext(Cell<E> next) {
this.next = next;
}
}
The class is now declared as
Cell<E>
(which is read as "
Cell
of
E
").
E
represents the type of element that a cell object can hold. In this gen-
eric version of
Cell
, everywhere that
Object
was used in the original
Cell
class now uses the name
E
.
E
is known as a
type variable
for which
a concrete type can be substituted. There's nothing special about the
name
E
it could be
ElementType
, for examplebut
E
is a nice abbreviation for
"element." By convention, type variables have single character names:
E
for an element type,
K
for a key type,
V
for a value type,
T
for a general
type, and so forth.
To create an actual
Cell
object you have to tell the compiler what spe-
cific type you wish to replace
E
with. For example, a
Cell
to hold a
String
could be constructed and referenced as follows:
Cell<String> strCell = new Cell<String>("Hello");