-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathUsingVariablesInLambdas.java
46 lines (41 loc) · 1.22 KB
/
UsingVariablesInLambdas.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package functionalProgramming;
/**
*
* @author chengfeili
* Jun 8, 2017 8:19:19 AM
*
* If you could add the final modifier to a local variable, it was
* “effectively final.” Lambdas use the same access rules as inner
* classes.
* Lambda expressions can access static variables, instance
* variables, effectively final method parameters, and effectively final
* local variables.
*/
interface Gorilla {
String move();
}
public class UsingVariablesInLambdas {
String walk = "walk";
void everyonePlay(boolean baby) {
String approach = "amble";
// approach = "run";
// instance variable
play(() -> walk);
// method parameter. It is effectively final since there are no
// reassignments to that variable.
play(() -> baby ? "hitch a ride" : "run");
/*
* effectively final local variable If we uncomment line 6, there will
* be a reassignment and the variable will no longer be effectively
* final. This would cause a compiler error.
*/
play(() -> approach);
}
void play(Gorilla g) {
System.out.println(g.move());
}
public static void main(String[] args) {
UsingVariablesInLambdas u = new UsingVariablesInLambdas();
u.everyonePlay(true);
}
}