I have the below scenario.
I would like to use a variable inside lambda expression. But this variable will be have a onetime value(final) based on a condition.
final String constraintsAmount;
if(constraint.isPresent()) {
constraintsAmount = constraint.getValue();
}
After this I start iterating over a list using forEach + lambda expression.
Now I have to use this constraintsAmount
field inside this iteration.
But it says that "constraintsAmount
might not have been initialized".
How can I get around this one.
Note :
- I don't want to declare this variable as an instance variable and I certainly don't want to declare and initialize this variable inside the iteration.
- Since it is a final I cant initialize it and then reuse it inside the if check. So wanted to check what is the work around.