I was working with CompletableFutures
when I ran in to situation. Let's say I wanted to do some clean up when an asynchronous operation completed. Is there an important difference between calling a member function in this way? Why would I choose the first example over the second?
Take these two examples:
someAsyncOperation.apply(foo).whenComplete(MyClass::handleOperationCompletion);
Versus:
someAsyncOperation.apply(foo).whenComplete((results, ex) -> handleOperationCompletion(results, ex));
Calling the static function, from an anonymous function, let's me add additional arguments in the local scope. In this second case I could add a local variable:
final Closeable object = new SomeCloseable();
someAsyncOperation.apply(foo).whenComplete((results, ex) -> handleOperationCompletion(results, ex, object));
I feel like this could be useful if I need keep context on some object to perform clean up (e.g like close a stream), and I can still write unit tests for it. Is there a trade off I'm missing here?