Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change value of variable in forEach loop java 8 [duplicate]

Tags:

java

java-8

int count =0;
requestUserDto.getNodeIds().forEach(userNodeId->{
    count++; // this statement shows error
     // some another statements here
    }
});

why count++ shows error

Error:

Local variable i defined in an enclosing scope must be final or effectively final

like image 439
Arihant Jain Avatar asked Oct 23 '25 15:10

Arihant Jain


1 Answers

Considering lambdas are essentially syntactic sugar for an anonymous inner class, a variable used in a lambda expression must be effectively final. Check out this post for more details.

like image 87
Ravindra Ranwala Avatar answered Oct 26 '25 04:10

Ravindra Ranwala



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!