I am learning Java generics from a book. The book says that "Class Type Variables Are Not Valid in Static Contexts" and explains it with the following example.
Consider a generic class with type variables, such as Entry. You cannot use the type variables K and V with static variables or methods. For example, the following does not work:
public class Entry<K, V> {
// Compiler error - V in static context ("Entry.this' cannot be referenced from a static context")
private static V defaultValue;
// Compiler error - V in static context ("Entry.this' cannot be referenced from a static context")
public static void setDefault(V value) {
defaultValue = value;
}
}
After all, type erasure means there is only one such variable or method in the erased Entry class, and not one for each K and V.
I don't understand the above explanation. I tried to create the same code for K also and I got the same compile errors. Why is the above code illegal ?