We know that a lambda expression can refer to and use static instance variables, instance variables and local variables( if they are effectively final). All this seems ok. Whenever I see any session on Lambdas and Java's take on functional programming I see one common point which is "Writing concurrent code is tough and so adapting functional code helps". However if I can access Static and instance variables in a lambda does this not defeat this point altogether. I know we have parallel streams which are very helpful in certain concurrent case but still is the closure scope not broken in Java if we are going towards functional programming style.
Also we should be creating pure functions in a functional style of programming. However if I am dependent on the state of the instance then my function is not pure.
I might be incorrect in inferring what I have shared but if I am then was there a particular reason to allow these design principles.
public class UsingStaticVariables {
static int staticTest = 10;
public static void main(String args[]) {
staticTest++;
System.out.println("Static test first value is " + staticTest);
Supplier<Integer> supplier = () -> {return staticTest++; };
staticTest++;
System.out.println("Static test second value is " + staticTest);
System.out.println("Static Test lambda output is " + supplier.get());
}
}
Supplier<Integer> supplier = () -> staticTest++;