Why does this Java method appear to have two return types?
Solution 1:
No, you don't have two return types.It's a generic method you are seeing.
<E extends Foo> --> you are declaring a generic type for your method
List<E> --> this is your return type
Your method can have a generic type E
which is a sub-class of Foo
. your return type is a List<Foo or any SubType Of FOO>
Solution 2:
The return type is List<E>
. The clause <E extends Foo>
is not a return type; it is a generic type declaration, specifying that the particular type E
must be a Foo
(or a subclass of Foo
). This is standard syntax for declaring a generic method.
Solution 3:
Take a look at the Java documentation pertaining to generics.
<E extends Foo> // declares the bounds for the generic type `E`
List<E> // declares the return value
The return type of the method is List<E>
.