What you're asking about is type inferrence.
Since it's a static method it has to infer the Generic type from somewhere; You don't have an instance of the class. That's what the <T>
means.
In the case of your method that takes no arguments it's actually inferring it from the target of the assignment. For example, say your method looked like this:
public static <T> List<T> getNewList() {
return new ArrayList<T>();
}
When using this method, T
is inferred from the target (in this case String
):
List<String> myList = MyClass.getNewList();
In your other static method where you have a Generic argument, T
is being inferred from the type being passed in:
public static <T> List<T> getNewListWithElement(T element) {
List<T> list = new ArrayList<T>();
list.add(element);
return list;
}
Here, if you tried doing:
List<String> myList = MyClass.getNewListWithElement(new Integer(4));
It would tell you that your target type was wrong, and you needed a List<Integer>
Specifically this is covered in sections 15.12.2.7 and 15.12.2.8 of the JLS.