0

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)
Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
ilovetolearn
  • 2,006
  • 5
  • 33
  • 64
  • 5
    These both mean exactly the same as they would mean without the generics. https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html – Dawood ibn Kareem Jun 06 '17 at 03:46
  • The generic parameters (``/``) are separate from the method modifiers (`public`/`static`), which are seperate from the return values (`int`/`void`). – Rogue Jun 06 '17 at 03:49

1 Answers1

2

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

f.khantsis
  • 3,256
  • 5
  • 50
  • 67