Forcing Java lambda expressions to capture non-final variables in Java
问题 Is it possible to have a lambda expression capture a variable that is not effectively final? I know that my code would work perfectly if it could capture them, currently I have to create a new variable that copies the non-final variable I want to pass into a lambda expression. final SomeClass otherObj; for(int i = 0; i < 10; i++) { int a = i; someObject.method(() -> otherObj.process(a, a+1)) } I'd like to pass in 'i' instead of having to create a new temporary variable. 回答1: You can try this