tk.hongkailiu.test.app.java8.lambda.scope.LambdaScope Maven / Gradle / Ivy
The newest version!
package tk.hongkailiu.test.app.java8.lambda.scope;
import java.util.function.Consumer;
/**
* Created by hongkailiu on 2015-04-25.
*/
public class LambdaScope {
public int x = 0;
class FirstLevel {
public int x = 1;
void methodInFirstLevel(int x) {
// The following statement causes the compiler to generate
// the error "local variables referenced from a lambda expression
// must be final or effectively final" in statement A:
//
// x = 99;
Consumer myConsumer = (y) -> {
System.out.println("x = " + x); // Statement A
System.out.println("y = " + y);
System.out.println("this.x = " + this.x);
System.out.println("LambdaScope.this.x = " + LambdaScope.this.x);
};
myConsumer.accept(x);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy