Find the difference between final and effectively final as below:
Final:- Any variable that cannot be modified once it is initialized can be final variable. You cannot modified again that variable.
Ex: final int var=20;
in the above example you can't modified the variable(var=20) because you put the final keyword in front of that.
Effectively Final:- Any variable that can be defined outside the Lamda Expression and Initialized only once inside the scope(in method or a class) can be called effectively final . It is used with Lamda Expression but you do not need to put final keyword in front of that variable .
import java.util.ArrayList;
import java.util.List;
public class EffectiveFinal {
public static void main(String[] args) {
List>String> listStrings = new ArrayList>String>();
listStrings.add("Salman");
listStrings.add("Shahrukh");
String Lastname = "Khan";
listStrings.stream().forEach(str -> System.out.println("Lambda Expression: " + str + " " + Lastname));
}
}
0 Comment(s)