final variable: A variable or parameter is declared as final and whose value is never changed after it is initialized is final.
effectively final variable: A variable or parameter is not declared as final and still the whose value is never changed after it is initialized is effectively final.
public class Demo {
public static void main(String[] args) {
int count = 0;
List list = new ArrayList() {{
add("employee1");
add("employee2");
add("employee3");
add("employee4");
}};
list.stream().forEach(str -> {
count++;
});
}
}
public static void main(String[] args) {
int count = 0;
List
here the error on count++ :-
Local variable count defined in an enclosing scope must be final or effectively final.
here the variable count is not declared as final but it is still a final variable and its value will not be changed, the count varibale here is effectively final variable
here the variable count is not declared as final but it is still a final variable and its value will not be changed, the count varibale here is effectively final variable
No comments:
Post a Comment