When I am working with optional class of java like below
Integer total = null;
Optional<Integer> b = Optional.of(new Integer(10));
b.ifPresent(b -> total =b);
The above code is not working(Error: java: local variables referenced from a lambda expression must be final or effectively final) but, when I use the AtomicInteger, It will work. Why this happens?
Optional<Integer> b = Optional.of(new Integer(10));
AtomicInteger total = new AtomicInteger();
b.ifPresent(b -> total.set(b));