riorio
riorio

Reputation: 6816

CompletableFuture.runAsync(() ->... with a non-final variable

I have this code that works fine:

String name = "Oscar";
CompletableFuture.runAsync(() -> doX(name));

Now I need to add some logic to the name variable:

String name = "Oscar";
if (x){
  name = "Tiger";
}
CompletableFuture.runAsync(() -> doX(name));

But now the compiler complains about Variable used in lambda expression should be final or effectively final

I understand from posts like this one that the name must be final or effectively final, but I wonder if there is a way to write the code differently in order to enable the logic on the name variable

Upvotes: 1

Views: 2285

Answers (3)

J-Alex
J-Alex

Reputation: 7107

Try to decouple resolver logic from the main flow:

public static void somewhere() {
    // Variable even may be explicitly final
    final String name = resolve(true);
    CompletableFuture.runAsync(() -> doX(name));
}

public String resolve(boolean condition) {
    if (condition)
        return "NameX";

    return "NameY";

    // Or:
    // return condition ? "NameX" : "NameY";
}

The advantage of the approach that you may add more complex conditions and can change this logic later without touching the original flow.

Upvotes: 0

HRgiger
HRgiger

Reputation: 2790

You can use Conditional Operator, something like this:

        boolean condition = true;
        String name = condition ? "Tiger" : "Oscar";
        CompletableFuture.runAsync(() -> System.out.println(name));

Or using if statements:

 boolean condition = true;
    final String name;
    if(condition) {
        name = "Tiger";
    }else {
        name = "Oscar";
    }
    CompletableFuture.runAsync(() -> System.out.println(name));

Upvotes: 2

vavasthi
vavasthi

Reputation: 952

The lambda expression needs a final variable. If the initialization code is complicated enough then define a new final variable.

final String fName = name;
CompletableFuture.runAsync(() -> doX(fName));

Upvotes: 0

Related Questions