In this static function in Java, why must <K, V> be repeated in line 1?
public static <K, V> HashMap<K, V> newInstance() {
return new HashMap<K, V>();
}
I understand why HashMap<K, V> is necessary since the function returns a HashMap with generic types K and V as keys and values respectively. However, why is the first <K, V> necessary in the function signature?
To indicate that the method is a generic method, using/returning generic types. If they were not there, the compiler would look for a "real type" (i.e. a class or an interface) named K, and another real type named V.