java - 为什么在这种情况下我必须使用final?

标签 java final

情况如下:

import java.io.File;

public class FinalTest1 {
    public static void main(String[] args) {
        FinalTest1 finalTest1 = new FinalTest1();
        finalTest1.test();
    }

    public void test(){
        File fileToBeModifiedFile = new File("AFile");
        Thread thread = new Thread(new Runnable() { 
            @Override
            public void run() {
                // TODO Auto-generated method stub
                modifyFile(fileToBeModifiedFile);
            }
        });

        thread.start();
    }

    public void modifyFile(File file){
        System.out.println("I'm going to modify the file");
    }
}

对于这种情况:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class FinalTest2{

    public void createUI(){
        JFrame frame = new JFrame("Final Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);

        JPanel mainPanel = new JPanel();
        JButton button = new JButton("Button");

        String string = "I am a string";

        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                JOptionPane.showMessageDialog(null, string);
            }
        });
        mainPanel.add(button,BorderLayout.CENTER);

        frame.add(mainPanel,BorderLayout.CENTER);

        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

    }

    public static void main(String[] args) {
        FinalTest2 finalTest = new FinalTest2();
        finalTest.createUI();
    }
}

我发现了这些情况的一些共同点。

1.需要final的变量是该方法的所有局部变量。

2.局部变量都在匿名类中被引用。

实际上RunnableActionListener都是接口(interface)。有关系吗?我在 https://stackoverflow.com/a/1299889/3378204 找到了答案,但我发现答案下面的评论中有很长的讨论。我现在有点困惑,能否请您帮我澄清一下,提前谢谢您。

最佳答案

局部变量只要保留在作用域内就会持续存在。如果该局部变量超出范围,它将被清除。如果在局部变量消失后调用匿名函数,则当它尝试使用不再存在的变量时,将导致未定义的行为。这就是问题所在。通过将局部变量设置为最终变量,编译器会跟踪该值而不是该变量,并将该值内联到匿名函数中,而不是指向该值的指针。

关于java - 为什么在这种情况下我必须使用final?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24522418/

相关文章:

java - 如何提高 Kabeja 生成的图像质量?

java - Rest FB 在 Servlet 中抛出异常

java - 为什么我可以匿名子类化枚举而不是最终类?

java - 最终变量和构造函数重载

java - 为什么这个缓存不能使用 final 作为修饰符

java - 为什么这个 Joda-Time 相关测试有时会失败?

Java 代码可以工作,但不确定是否满足要求(对象和类)

java - java序列化如何在未指定默认构造函数时反序列化最终字段?

Java8 : Is there a way to get an instance method reference from a class method reference?

java - 无法在内部类中声明 Public static final String s = new String ("123")