-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodReferenceExample.java
More file actions
42 lines (33 loc) · 1.09 KB
/
MethodReferenceExample.java
File metadata and controls
42 lines (33 loc) · 1.09 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
package method_reference_and_constructor_reference;
/*
* Example demonstrating lambda expression and equivalent instance method reference
* and static method reference
*/
public class MethodReferenceExample {
static void staticMethod(){
for(int i=1;i<=10;i++){
System.out.println("static method ref: "+i);
}
}
void instanceMethod(){
for(int i= 1; i<=10; i++){
System.out.println("instance method ref: "+i);
}
}
public static void main(String[] args) {
// Thread Creation using lambda expression
Thread t1 = new Thread(()->{
for (int i=1; i<=10; i++){
System.out.println("Lambda Expression: "+i);
}
});
// Thread creation using static method ref.
Thread t2 = new Thread(MethodReferenceExample::staticMethod);
MethodReferenceExample ob = new MethodReferenceExample();
// Thread creation using instance method ref.
Thread t3 = new Thread(ob::instanceMethod);
t1.start();
t2.start();
t3.start();
}
}