advocateofnone
advocateofnone

Reputation: 2541

What is the meaning of the following method definition?

What does the first part in the following method definition?

<I, O> MyReturnType<I, O> myMethod() { ... }

The second is the method'sreturn type, third is the method name, but what is the first one?

Upvotes: 2

Views: 326

Answers (1)

stealthjong
stealthjong

Reputation: 11093

I and O are declared as generic type parameters. They're generic types introduced by the method itself, as said here: https://docs.oracle.com/javase/tutorial/java/generics/methods.html

Generic methods are methods that introduce their own type parameters. This is similar to declaring a generic type, but the type parameter's scope is limited to the method where it is declared. Static and non-static generic methods are allowed, as well as generic class constructors.

If you don't declare <I, O>, java will look for types called I and O (which won't be there, since they're supposed to be generic).

I think @khelwood put it nicely (see comments on original question): It's saying: "In the following definition, I and O are standing in for some types that depend on the situation when the method is called."

Upvotes: 2

Related Questions