-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample4.java
More file actions
67 lines (61 loc) · 1.78 KB
/
Example4.java
File metadata and controls
67 lines (61 loc) · 1.78 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package lambda_expressions;
/*
* Example demonstrating closure concept.
* The referenced local variable of the enclosing method of lambda expression/anonymous function are implicitly final (not allowed to change).
* There is no need to explicitly specify them as final, but they are implicitly final in nature.
*/
@FunctionalInterface
interface ClosureEx{
void m1();
}
public class Example4{
// instance variable
int x = 10;
public ClosureEx getLambdaImpl(){
// local variable : implicitly final
int y = 20;
// Lambda Expression
ClosureEx cx = () -> {
System.out.println("Lambda Expression implementation");
System.out.println(x); //10
System.out.println(y); //20
x = 30;
//y = 40; //compilation error
};
//y=50; //compilation error
return cx;
}
public ClosureEx getAnonymousClassImpl(){
// local variable : implicitly final
int y = 20;
//Anonymous Function
ClosureEx cx = new ClosureEx() {
@Override
public void m1() {
System.out.println("Anonymous class implementation");
System.out.println(x); //30
System.out.println(y); //20
x = 50;
//y = 40; //compilation error
}
};
//y=50; //compilation error
return cx;
}
public static void main(String[] args) {
Example4 obj = new Example4();
ClosureEx lambdaImpl = obj.getLambdaImpl();
ClosureEx anonymousImpl = obj.getAnonymousClassImpl();
lambdaImpl.m1();
anonymousImpl.m1();
}
}
/*
* Output:
* Lambda Expression implementation
* 10
* 20
* Anonymous class implementation
* 30
* 20
*/