Wildcards and Generic Methods in Java - Generic Methods
(Page 4 of 5 )
Thus far in this chapter, we've talked about generic types and the implementation of generic classes. Now, we're going to look at a different kind of generic animal: generic methods. Generic methods essentially do for individual methods what type parameters do for generic classes. But as we'll see, generic methods are smarter and can figure out their parameter types from their usage context, without having to be explicitly parameterized. (In reality, of course, it is the compiler that does this.) Generic methods can appear in any class (not just generic classes) and are very useful for a wide variety of applications.
First, let's quickly review the way that we've seen regular methods interact with generic types. We've seen that generic classes can contain methods that use type variables in their arguments and return types in order to adapt themselves to the parameterization of the class. We've also mentioned that generic types themselves can be used in most of the places that any other type can be used. So methods of generic or nongeneric classes can use generic types as argument and return types as well. Here are examples of those usages:
// Not generic methods
class GenericClass< T > {
// method using generic class parameter type
public void T cache( T entry ) { ... }
}
class RegularClass {
// method using concrete generic type
public List<Date> sortDates( List<Date> dates ) { ... }
// method using wildcard generic type
public List<?> reverse( List<?> dates ) { ... }
}
The cache() method in GenericClass accepts an argument of the parameter type T and also returns a value of type T. The sortDates() method, which appears in the nongeneric example class, works with a concrete generic type, and the reverse() method works with a wildcard instantiation of a generic type. These are examples of methods that work with generics, but they are not true generic methods.
Generic Methods Introduced
Like generic classes, generic methods have a parameter type declaration using the <> syntax. This syntax appears before the return type of the method:
// generic method
<T> T cache( T entry ) { ... }
This cache() method looks very much like our earlier example, except that it has its own parameter type declaration that defines the type variable T. This method is a generic method and can appear in either a generic or nongeneric class. The scope of T is limited to the method cache() and hides any definition of T in any enclosing generic class. As with generic classes, the type T can have bounds:
<T extends Entry & Cacheable > T cache( T entry ) { ... }
Unlike a generic class, it does not have to be instantiated with a specific parameter type for T before it is used. Instead, it infers the parameter type T from the type of its argument, entry. For example:
BlogEntry newBlogEntry = ...;
NewspaperEntry newNewspaperEntry = ...;
BlogEntry oldEntry = cache( newBlogEntry );
NewspaperEntry old = cache( newNewspaperEntry );
Here, our generic method cache() inferred the type BlogEntry (which we'll presume for the sake of the example is a type of Entry and Cacheable). BlogEntry became the type T of the return type and may have been used elsewhere internally by the method. In the next case, the cache() method was used on a different type of Entry and was able to return the new type in exactly the same way. That's what's powerful about generic methods: the ability to infer a parameter type from their usage context. We'll go into detail about that next.
Another difference with generic class components is that generic methods may be static:
class MathUtils {
public static <T extends Number> T max( T x, T y ) { ... }
}
Constructors for classes are essentially methods, too, and follow the same rules as generic methods, minus the return type.
Next: Type Inference from Arguments >>
More Java Articles
More By O'Reilly Media
|
This article was excerpted from chapter eight of the book Learning Java, third edition, written by Patrick Niemeyer and Jonathan Knudsen (O'Reilly; ISBN: 0596008732). Check it out today at your favorite bookstore. Buy this book now.
|
|