Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
Streams
Effectively final variable.
1
5
6
7
8
9
10
11
15
// {
//Incorrect
for (int i = 0; i < 10; i++) {
new Thread(() -> {
System.out.println("i = " + i); // Does not compile!
}).start();
}
// {
Enter to Rename, Shift+Enter to Preview
1
5
6
7
8
9
13
// {
for (int i = 0; i < 10; i++) {
int j = i; //effectively final
new Thread(() -> System.out.println("i = " + j)).start();
}
// {
Enter to Rename, Shift+Enter to Preview
1
5
6
7
8
9
10
11
15
// {
//Incorrect
for (int i = 0; i < 10; i++) {
int j = i;
new Thread(() -> System.out.println("i = " + j)).start();
j++;
}
// {
Enter to Rename, Shift+Enter to Preview
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content