[英]local variable iteration defined in an enclosing scope must be final or effectively final
Whenever I try to compile the program, I get the error "local variable iteration defined in an enclosing scope must be final or effectively final".每当我尝试编译程序时,都会收到错误消息“在封闭的 scope 中定义的局部变量迭代必须是最终的或有效的最终”。 Does anyone know how to fix this?
有谁知道如何解决这一问题? The code is not completed yet, so some things may seem out of place.
代码还没有完成,所以有些事情可能看起来不合适。
public static void go(purchase joe) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SpinnerNumberModel NumberImput =
new SpinnerNumberModel(1.00,0.00,null,1.00);
int iteration = 0;
JSpinner spinner = new JSpinner(NumberImput);
JLabel label = new JLabel("Please enter the price of the Item.");
JButton button = new JButton("Press here to continue");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
switch (iteration) {
case 0:
joe.setPrice((double)NumberImput.getNumber());
label.setText("Plese enter the amount that you are buying");
NumberImput.setValue(1.00);
iteration += 1;
break;
case 1:
joe.setAmount((int)NumberImput.getNumber());
label.setText("Plese enter the amount that you are buying");
break;
}
}
});
frame.setLayout(new FlowLayout());
frame.add(label);
frame.add(spinner);
frame.add(button);
frame.setSize(600, 500);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
joe.setPrice((double)NumberImput.getNumber());
}
You are using iteration += 1;
您正在使用
iteration += 1;
. . This is an assignment to a local variable.
这是对局部变量的赋值。 Now the ActionListener object lives longer than the local variable;
现在 ActionListener object 比局部变量的寿命更长; actionPerformed may be called after the method's call disappeared.
actionPerformed 可能在方法的调用消失后被调用。
For this reason the Java Virtual Machine adds a new variable iteration
to the ActionListener, a copy.出于这个原因,Java 虚拟机向 ActionListener 添加了一个新的变量
iteration
,一个副本。 To prevent that assigning to the local variable iteration
would make the other iteration
stale, the language decided that the local variable must be constant, not assigned to, "effectively final."为了防止分配给局部变量
iteration
会使另一个iteration
过时,该语言决定局部变量必须是常量,而不是分配给“有效地最终”。
AtomicInteger iteration = new AtomicInteger();
... iteration.getAndIncrement();
However as actionPerformed
is called later, just remove iteration
.但是,由于稍后会调用
actionPerformed
,因此只需删除iteration
即可。 You could use a field in ActionListener.您可以在 ActionListener 中使用一个字段。
button.addActionListener(new ActionListener() {
int iter;
@Override
public void actionPerformed(ActionEvent e) {
switch (iter) {
case 0:
joe.setPrice(NumberImput.getNumber().doubleValue());
label.setText("Please enter the amount that you are buying");
NumberImput.setValue(1.00);
iter++;
break;
case 1:
joe.setAmount(NumberImput.getNumber().intValue());
label.setText("Please enter the amount that you are buying");
break;
}
}
});
On style: always use @Override
as you will capture typos in similar cases.关于风格:始终使用
@Override
,因为您会在类似情况下捕获拼写错误。
@Override
public void actionPerformed(ActionEvent e){
Use numberInput
;使用
numberInput
; variables, fields and methods starting with a small letter.以小写字母开头的变量、字段和方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.