在 Java 中引用匿名内部类中封闭类的非最终字段

发布于 2024-08-21 09:57:27 字数 849 浏览 1 评论 0原文

在Java中,我知道可以做这样的事情:

public class Greeter {
    public void greetEventually() {
        final String greeting = "Hello!";
        Job j = new Job() {
            public void run() {
                System.out.println(greeting);
            }
        };
        j.schedule();
    }
}

这将在将来的某个时刻执行匿名Job。这是可行的,因为匿名类可以引用封闭范围内的最终变量。

我不确定的是以下情况:

public class Greeter {
    private String greeting;

    // ... Other methods that might mutate greeting ...

    public void greetEventually() {
        Job j = new Job() {
            public void run() {
                System.out.println(greeting);
            }
        };
        j.schedule();
    }
}

在这种情况下,我的匿名 Job 引用了封闭类的非最终字段。当作业运行时,我会看到创建作业时的欢迎字段值还是执行作业时的值吗?我想我知道答案,但我认为这是一个有趣的问题,起初它让我和几个同事对自己进行了几分钟的反思。

In Java, I know that it is possible to do something like this:

public class Greeter {
    public void greetEventually() {
        final String greeting = "Hello!";
        Job j = new Job() {
            public void run() {
                System.out.println(greeting);
            }
        };
        j.schedule();
    }
}

This would execute the anonymous Job at some point in the future. This works because anonymous classes are allowed to refer to final variables in the enclosing scope.

What I'm not sure about is the following case:

public class Greeter {
    private String greeting;

    // ... Other methods that might mutate greeting ...

    public void greetEventually() {
        Job j = new Job() {
            public void run() {
                System.out.println(greeting);
            }
        };
        j.schedule();
    }
}

In this case my anonymous Job is referring to a non-final field of the enclosing class. When the Job runs, will I see the value of the greeting field as it was when the Job was created, or as it is when it is executing? I think I know the answer, but I thought it was an interesting question, and at first it left me and a couple of coworkers second-guessing ourselves for a few minutes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

您将看到执行匿名 Jobgreeting 的值。

仅局部变量需要 final 修饰符,成员变量不需要。

You'll see the value of greeting as it is when the anonymous Job executes.

The final modifier is required only for local variables, not member variables.

执妄 2024-08-28 09:57:27

您正在通过(外部)this 访问该字段。您可以将 this 实际上视为一个 final 局部变量。只有本地是final,指向的对象不是(必然)常量。想象一个与 this 具有相同值的局部变量,它应该很清楚。

public class Greeter {
    private String greeting;

    // ... Other methods that might mutate greeting ...

    public void greetEventually() {

        private final Greeter greeter = this; // <---

        Job j = new Job() {
            public void run() {
                System.out.println(   greeter.greeting   ); // <---
            }
        };
        j.schedule();
    }
}

You are accessing the field through (the outer) this). You can think of this as effectively a final local variable. Only the local is final, the object pointed to is not (necessarily) constant. Imagine a local variable with the same value as this and it should be clear.

public class Greeter {
    private String greeting;

    // ... Other methods that might mutate greeting ...

    public void greetEventually() {

        private final Greeter greeter = this; // <---

        Job j = new Job() {
            public void run() {
                System.out.println(   greeter.greeting   ); // <---
            }
        };
        j.schedule();
    }
}
┈┾☆殇 2024-08-28 09:57:27

Final修饰符应用于局部变量只是为了为内部类的每个实例提供变量,因此我们使用:
最后的字符串问候语;

当您只需要变量的一个实例时(例如常量或公共资源的情况),请使用:
私人字符串问候语;

The final modifier is applied to the local variables only to provide variables for each instance of the inner class, so we use:
final String greeting;

When you need only one instance of the variable (like the case of constants or common resources), use:
private String greeting;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文