Skip to content
Advertisement

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?

Advertisement

Answer

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

1 People found this is helpful
Advertisement