Parametric Polymorphism in Static Methods
The types used by a static method can include type parameters, just as with dynamic methods.
With dynamic methods, however, the type parameters are within
the scope of type parameters declared for the class.
Within a IMap<K,V>
class, for example,
dynamic methods can use type parameters K
and V
.
In Java, the type parameters declared with the class name are actually associated with objects, not with the class. (That allows different objects of the class to have different types for the type parameters.)
Because the type parameters declared with the class name are associated with objects, the class's static methods are not within the scope of those type parameters. If a static method refers to type parameters that do not occur within the types of its arguments, those type parameters must be declared by naming them within angle brackets immediately before the return type of the static method.
Example.
The following static factory might be defined within an
IMaps<K,V>
class associated with an
IMap<K,V>
type of immutable finite
functions:
public static <K,V> IMap<K,V> empty () { ... }
If the <K,V>
declaration that comes
after static
is omitted, the Java compiler
is likely to generate an error message that is hard to
understand.
If you are writing a polymorphic static method and get
an error message you don't understand, this is one of
the things you should check.