关于lambda表达式中出现的变量

public class streamDemo {
    //定一个方法,方法的参数传递Supplier,泛型使用Integer
    public static int getMax(Supplier<Integer> sup){
        return sup.get();
    }
    public static void main(String[] args) {
        int[] arr = {2, 3, 4, 52, 333, 23};
        //调用getMax方法,参数传递Lambda
        int maxNum = getMax(()->{
        //计算数组的最大值
            int max = arr[0];
            for(int i : arr){
                if(i>max){
                    max = i;
                }
            }
            return max;
        });
        System.out.println(maxNum);
    }
}

在lambda中,使用了main方法中的局部变量,但是在调用这个getMax方法时,又是如何能够访问到main中的变量的呢?

返回值当参数传进去吧

https://www.jianshu.com/p/7e015bde3128