-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLambdaVariable1.java
46 lines (34 loc) · 1.06 KB
/
LambdaVariable1.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 com.learn.lambdas;
import java.util.function.Consumer;
public class LambdaVariable1 {
private static int num = 10;
public static void main(String[] args) {
int i = 10; // local Variable
/**
* we can't use the same variable which is
* already defined in this method scope.
*/
Consumer<Integer> consumer1 = (i1) -> {
System.out.println(i1);
};
int value = 5;
/**
* we are not allowed to modify any local variables
* that is being referenced in lambda scope.
*/
Consumer<Integer> consumer2 = (i1) -> {
//i = 10; ( Not allowed )
System.out.println(i1 + value);
};
// value = 5; even we can't do this. since it is referenced in lambda scope.
/**
* For Instance Variables or Class Variables,
* there is no restriction.
*/
Consumer<Integer> consumer3 = (i1) -> {
num = 5;
System.out.println(i1 + num);
};
num = 2;
}
}