Java generic method declaration
In the latter you have a reference to the type within the scope of someMethod
, namely E
. In the former you do not.
The main difference is that the latter is a generic method the former is not.
So for example in the latter method you can do something like this:
public static <E extends MyObject> void someMethod(List<E> someList) {
E myObject = someList.iterator().next(); // this can actually lead to errors
myObject.doSomething(); // so treat it as an example
}
This means that you can substitute an arbitrary type E
which conforms to the rule in the generic method declaration and be able to use that type in your method.
Be advised though that you should call the generic method with type arguments like this:
someClass.<MyArbitraryType>someMethod(someList);
You can find a nice overview of generic methods here.
With the second version you can do something like:
public static <E extends Number> void someMethod(List<E> numberList) {
E number = numberList.get(0);
numberList.add(number);
}
This isn't possible with the first version.