Your code is equivalent
static Function<Integer, Integer> fibLambda = null; public static void main(String[] args) { fibLambda = n -> n <= 2 ? 1 : Example.fibLambda.apply(n - 1) + Example.fibLambda.apply(n - 2); System.out.println(fibLambda.apply(6)); }
By the time apply is called fibLambda , a value is assigned. Basically, the lambda expression does not capture the value of fibLambda , it simply registers that the variable must be evaluated at the right time to get the value.
Remember that the lambda expression does not execute the code that appears in its body. This is just a declaration, similar to how you declare an instance of an anonymous class.
source share