'Why does this Java method appear to have two return types?
public <E extends Foo> List<E> getResult(String s);
where Foo is my own class.
What is the return type of this method? Why does it seem to have two return types?
Solution 1:[1]
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 2:[2]
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>.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | rgettman |
| Solution 2 | Fls'Zen |
