Your variable x would be permanently final, it would be initialized once, and under no circumstances would it be changed. If you only have:
int x = 0; doLater(() -> showErrorMessage(x));
then it compiled.
However, adding conditions that can change the value of a variable
int x = 0; if (isA()) { x = 1; } else if (isB()) { x = 2; }
makes the variable ineffective final, and therefore the compilation error increases.
In addition, since this pointer approach that you implemented will not work, you can rebuild your code a bit into a simple if-else statement:
if (isA()) { doLater(() -> showErrorMessage(1)); } else if (isB()) { doLater(() -> showErrorMessage(2)); }
and completely get rid of x .
Konstantin yovkov
source share