Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this Java method appear to have two return types?

Tags:

java

methods

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?

like image 244
Cacheing Avatar asked Mar 07 '13 21:03

Cacheing


3 Answers

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 subclass of Foo. The return type of the method is a List<Foo-or-any-subtype-of-Foo>.

like image 180
PermGenError Avatar answered Sep 20 '22 11:09

PermGenError


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.

like image 43
rgettman Avatar answered Sep 19 '22 11:09

rgettman


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>.

like image 41
Fls'Zen Avatar answered Sep 19 '22 11:09

Fls'Zen