Steve Cohen
Steve Cohen

Reputation: 4859

Need Help Understanding this generic declaration

Below is a method from java.util.concurrent.ScheduledExecutorService:

/**
 * Creates and executes a ScheduledFuture that becomes enabled after the
 * given delay.
 *
 * @param callable the function to execute
 * @param delay the time from now to delay execution
 * @param unit the time unit of the delay parameter
 * @return a ScheduledFuture that can be used to extract result or cancel
 * @throws RejectedExecutionException if the task cannot be
 *         scheduled for execution
 * @throws NullPointerException if callable is null
 */
public <V> ScheduledFuture<V> schedule(Callable<V> callable,
                                       long delay, TimeUnit unit);

Why is there <V> ScheduledFuture<V>?

This looks at first glance like two return types on the method. So, if V is, let's say Boolean, and we supply a Callable<Boolean> as the first parameter, what is the return type of the method? Is it Boolean, ScheduledFuture<Boolean> or something else?

Please, someone, unpack this for me.

Upvotes: 2

Views: 649

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1504182

Why is there <V> ScheduledFuture<V>?

Because that's the type parameter and the return type.

The <V> part isn't a return type, it's just saying "This is a generic method with a single type parameter, V."

So we have:

public                                            // Access modifier
<V>                                               // Type parameter
ScheduledFuture<V>                                // Return type
schedule                                          // Method name
(Callable<V> callable, long delay, TimeUnit unit) // Parameters

Upvotes: 3

Related Questions