At line names.forEach(s -> count++); of the below program, the compiler is giving error - "Local variable count defined in an enclosing scope must be final or effectively final".
How can this error be resolved?
Note: The requirement is not to find the size of the list. It's just an example of the error. I, actually, want to understand the issue and know how it can be addressed in the below program.
public static void main(String[] args) {
List<String> names = new ArrayList<>();
names.add("Tom");
names.add("Dick");
names.add("Harry");
int count=0;
names.forEach(s -> count++); //ERROR IN THIS LINE
System.out.println("Size of List = "+count);
}