情况如下:
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.局部变量都在匿名类中被引用。
实际上Runnable和ActionListener都是接口(interface)。有关系吗?我在 https://stackoverflow.com/a/1299889/3378204 找到了答案,但我发现答案下面的评论中有很长的讨论。我现在有点困惑,能否请您帮我澄清一下,提前谢谢您。
最佳答案
局部变量只要保留在作用域内就会持续存在。如果该局部变量超出范围,它将被清除。如果在局部变量消失后调用匿名函数,则当它尝试使用不再存在的变量时,将导致未定义的行为。这就是问题所在。通过将局部变量设置为最终变量,编译器会跟踪该值而不是该变量,并将该值内联到匿名函数中,而不是指向该值的指针。
关于java - 为什么在这种情况下我必须使用final?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24522418/