Java Reference
In-Depth Information
ers or return types that correspond to a class type parameter, and you must not use the class type parameters
in the bodies of static method definitions.
This does not mean that static methods cannot be parameterized — I am talking only about types for
variables in a static method corresponding to any of the parameter types in a generic type definition. You
see later in this chapter that you can define generic methods that have their own independent parameterized
definitions involving their own set of parameters, and such parameterized methods may be static or non-stat-
ic.
Static Fields in a Generic Type
All types produced from a given generic type share the same runtime type. Consequently they do not
have their own independent static fields. Any static fields you define in a generic type are shared among
all instances of it. For example, suppose you add a static field — count , say — of type int to the
LinkedList<> type definition to record the number of objects created. You then add a statement to each
constructor to increment count each time it was called. Each type instance, such as LinkedList<String>
or LinkedList<Point> , would share a single copy of count , so the static count member of the
LinkedList<String> type would reflect the number of times the LinkedList<T> constructor had been
called; in our example this would be two. Static fields in generic type definitions are best avoided.
Type Parameter Bounds
In some situations you define a generic type where you want to constrain the type arguments that are sup-
plied to define a class instance so that they extend a particular class, or implement specific interfaces, or
even both. The reason for this is that your generic type has to make some assumptions about the capabilities
of the objects an instance of the type is dealing with. Such constraints are called type parameter bounds .
The first bound that you specify for a type parameter can be either a class type or an interface type. Any
additional bounds after the first for a type parameter can be interface types only. If you don't specify any
bounds for a type parameter, it has type Object as its implicit bound because all classes have this type as
their ultimate base class.
To understand how you specify a type parameter bound, consider a simple example of where this applies.
Suppose that you want to modify the LinkedList<T> generic type that you saw earlier so that objects of
classes produced by this type would be serializable. Not only would LinkedList<T> need to implement the
Serializable interface, but type T would also have to implement Serializable . The definition for the
generic type would therefore look like this:
import java.io.Serializable;
class LinkedList<T extends Serializable> implements Serializable {
// Default constructor - creates an empty list
public LinkedList() {}
// Constructor to create a list containing one object
public LinkedList(T item) {
if(item != null) {
current=end=start=new ListItem(item); // item is the start and end
}
}
// Construct a linked list from an array of objects
Search WWH ::




Custom Search