This question already has answers here:
What does the generic nature of the class Class<T> mean? What is T?
(4 answers)
How to interpret Java generics like <T> T , <T,V> Query<T> , Class<T>?
(4 answers)
What does 'public static void' mean in Java?
(9 answers)
What does it mean for a method to be public/private/other in java? [closed]
(6 answers)
Closed 2 years ago.
I have been seeing the following methods declaration but I do not understand how <K, V>
and <T>
are being used.
What does public <K, V>
does in this method?
public <K, V> void add(K k, V v)
What does static <T>
does in this method?
public static <T> int countGreaterThan(T[] anArray, T elem)
Recall, that in Java, all methods MUST be inside a class, so this method will be in some sort of class, lets say "Clazz".
public
indicates that this method can be accessed from outside the class, by absolutely anyone.
static
indicated that when you call this method, you don't need to call it from a specific object, but rather you can call it just by using the class name. So you would be able to call countGreaterThen by typing Clazz.countGreaterThan
instead of using a specific object created as an instance of Clazz