简体   繁体   English

lambda表达式和全局变量

[英]lambda expression and global variables

I just started working around lambda expression in Java 8. I'm bit confused about effectively final . 我刚刚开始研究Java 8中的lambda表达式。我对effectively final感到有些困惑。

Local variable initialized once is effectively final but what about global variable, I can change value of global variable and use it in lambda expression. 一次初始化的局部变量effectively finaleffectively final变量,但是全局变量呢,我可以更改全局变量的值并在lambda表达式中使用它。 So what is the reason behind local variable should be effectively final . 那么局部变量应该effectively final的原因是什么。 I couldn't find any articles or on javadoc about this. 我找不到关于此的任何文章或在javadoc上。

import java.util.ArrayList;
import java.util.List;

public class Main {

    private String lastname = "Thakor";

    public static void main(String[] args) {
        Main objMain = new Main();
        objMain.test();
    }

    public void test(){
        List<String> listStrings = new ArrayList<String>();
        listStrings.add("Vicky");
        listStrings.add("Thakor");

        String middlename = "V";
        //Local variable middlename defined in an enclosing scope must be final or effectively final
        /* middlename = "T";*/ 

        /**
         * In case of global variable, why lambda expression not throwing error...
         * Local variable middlename defined in an enclosing scope must be final or effectively final
         */
        lastname = "T"; 

        listStrings.stream()
                        .forEach(firstname ->{
                            System.out.println("Firstname: " + firstname);
                            System.out.println("Middle: " + middlename);
                            System.out.println("Lastname: " + lastname);
                            System.out.println("--------------------------------------");
                        });
    }
}

Output 产量

Firstname: Vicky
Middle: V
Lastname: T
--------------------------------------
Firstname: Thakor
Middle: V
Lastname: T
--------------------------------------

When you reference a "global" variable in a lambda, you're really capturing a reference to this , which is effectively final. 当您在lambda中引用“全局”变量时,实际上是在捕获this的引用, this实际上是最终的。

Consider the following snippet (untested): 考虑以下代码片段(未经测试):

class Main {

    class Box {
        int value;
    }

    void foo() {
        Box box = new Box();
        Runnable r = () -> box.value++;
    }
}

Even though you're changing the box.value , you're doing it through the box reference, which is effectively final. 即使您要更改box.valuebox.value通过box引用来完成,实际上是最终的。 Similarly, you code is equivalent to 同样,您的代码等效于

System.out.println("Lastname: " + this.lastname);

meaning you're accessing lastname through the this reference, which is effectively final. 表示您正在通过this引用访问lastname ,实际上是最终名称。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在lambda表达式中分配局部变量 - Assigning local variables inside a lambda expression 在表达式中使用外部不可变变量的Lambda表达式 - Lambda expressions with external immutable variables used within the expression 键入变量,方法内联和“lambda表达式中的错误返回类型” - Type variables, method inlining and “Bad return type in lambda expression” 从 lambda 表达式引用的局部变量必须是最终的或有效的最终变量 - local variables referenced from a lambda expression must be final or effectively final 从lambda表达式引用的局部变量必须是final - Local variables referenced from a lambda expression must be final Lambda表达式-使用Lambda表达式 - Lambda expressions - use lambda expression (Oracle 教程中的错别字?)从 lambda 表达式访问封闭范围的局部变量 - (Typos in Oracle tutorial?) Accessing local variables of the enclosing scope from a lambda expression 当我将lambda表达式作为参数传递它可以访问此范围内的其他变量时,怎么可能? - How is it possible that when I pass a lambda expression as a parameter it can access other variables in this scope? 事件处理程序有编译错误:“从 lambda 表达式引用的局部变量必须是最终的” - Event handler has compile error: “local variables referenced from a lambda expression must be final” Java:为什么java不能在封闭的lambda表达式范围内自动“完成”局部变量? - Java: why java can't automatically "finalize" local variables in enclosing scope of lambda expression?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM