简体   繁体   English

在封闭 scope 中定义的局部变量迭代必须是最终的或有效的最终

[英]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.

相关问题 在封闭作用域中定义的局部变量必须是最终的或有效的最终变量 - Local variable defined in an enclosing scope must be final or effectively final 在封闭 scope 中定义的局部变量 ObjList 必须是最终的或有效的最终 - Local variable ObjList defined in an enclosing scope must be final or effectively final 在封闭范围内定义的局部变量 log 必须是 final 或有效 final - Local variable log defined in an enclosing scope must be final or effectively final 线程:封闭范围中定义的局部变量必须是final或有效的final - Threads: Local variable defined in an enclosing scope must be final or effectively final 我在封闭作用域中定义的局部变量必须是最终的或有效的最终变量 - Local variable i defined in an enclosing scope must be final or effectively final 在封闭的 scope 中定义的局部变量 collect 必须是最终的或有效的最终的 - Local variable collect defined in an enclosing scope must be final or effectively final 封闭范围中定义的局部变量textfeild_1必须是final还是有效的final? - local variable textfeild_1 defined in an enclosing scope must be final or effectively final? 计时器错误的int值:在封闭范围内定义的局部变量ans必须是final或有效的final - int for timer error: Local variable ans defined in an enclosing scope must be final or effectively final 在封闭范围内定义的局部变量 statusMessage 在返回新对象时必须是最终的或有效的最终 - Local variable statusMessage defined in an enclosing scope must be final or effectively final on returning a new object Java 8 显示此错误。 在封闭作用域中定义的局部变量 itemList 必须是 final 或有效 final - Java 8 shows this error. Local variable itemList defined in an enclosing scope must be final or effectively final
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM