简体   繁体   English

无法分配最终的局部变量,因为它是在封闭类型中定义的

[英]The final local variable cannot be assigned, since it is defined in an enclosing type

ratingS = new JSlider(1, 5, 3); 
ratingS.setMajorTickSpacing(1);
ratingS.setPaintLabels(true);
int vote;

class SliderMoved implements ChangeListener {
    public void stateChanged(ChangeEvent e) {
        vote = ratingS.getValue();
    }
}

ratingS.addChangeListener(new SliderMoved());

If i write the above code Eclipse tells me this: 如果我写上面的代码Eclipse告诉我这个:

Cannot refer to a non-final variable vote inside an inner class defined in a different method 不能在不同方法中定义的内部类中引用非最终变量vote

But if i add final before int vote it gives me this error: 但如果我在int vote之前添加final ,它会给我这个错误:

The final local variable vote cannot be assigned, since it is defined in an enclosing type 无法分配最终的局部变量投票,因为它是在封闭类型中定义的

So, how to solve? 那么,如何解决?

Well, the standard trick is to use an int array of length one. 好吧,标准技巧是使用长度为1的int数组。 Make the var final and write to var[0] . 使var final并写入var[0] It is very important to make sure you don't create a data race. 确保不创建数据竞争非常重要。 Using your code as an example: 以您的代码为例:

final int[] vote = {0};

class SliderMoved implements ChangeListener {
  public void stateChanged(ChangeEvent e) {
    vote[0] = ratingS.getValue();
  }
}

Since all this will be happenenig on the EDT, including the callback invocation, you should be safe. 由于所有这些都将在EDT上发生,包括回调调用,你应该是安全的。 You should also consider using the anonymous class: 您还应该考虑使用匿名类:

ratingS.addChangeListener(new ChangeListener() {
  public void stateChanged(ChangeEvent e) { vote[0] = ratingS.getValue(); }
});

Move vote to SliderMoved : vote移至SliderMoved

class SliderMoved implements ChangeListener {
    private int vote;
    public void stateChanged(ChangeEvent e) {
        this.vote = ratingS.getValue();
        // do something with the vote, you can even access
        // methods and fields of the outer class
    }
    public int getVote() {
        return this.vote;
    }
}

SliderMoved sm = new SliderMoved();
ratingS.addChangeListener(sm);

// if you need access to the actual rating...
int value = rattingS.getValue();

// ...or
int value2 = sm.getVote();

EDIT 编辑

Or alternatively, pass a model class to the change listener 或者,将模型类传递给更改侦听器

public class Person {
    private String name;
    private int vote;
    public int getVote() {
        return this.vote;
    }
    public void setVote(int vote) {
        this.vote = vote;
    }
    // omitting other setter and getter
}

Person is used as follows: Person使用如下:

 class SliderMoved implements ChangeListener {
    private Person person;
    public SliderMoved(Person person) {
        this.person = person;
    }
    public void stateChanged(ChangeEvent e) {
        this.person.setVote(ratingS.getValue());
    }
    public Person getPerson() {
        return this.person;
    }
}

Person person = new Person();

ratingS.addChangeListener(new SliderMoved(person));

// access the vote
int vote = person.getVote();

我终于在主类中将声明声明为实例变量(私有)。

Setting the Compiler compliance level to 1.8 worked for me to solve a similar problem. 将编译器合规性级别设置为1.8对我来说可以解决类似的问题。 I don't understand the reason but you may try this. 我不明白原因,但你可以试试这个。

How it works: 这个怎么运作:

Right click on the project --> Properties --> Java Compiler. 右键单击项目 - > Properties - > Java Compiler。 And in java compiler properties window, set the compiler compliance level to 1.8. 在java编译器属性窗口中,将编译器合规性级别设置为1.8。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java:最终的局部变量无法分配,因为它是用封闭类型定义的 - Java: the final local variable cannot be assigned, since it was defined in an enclosing type 最终局部变量checkstate不能分配,因为它是用封闭类型定义的 - The final local variable checkstate cannot be assigned, since it is defined in an enclosing type 最终局部变量无法分配,因为它是用封闭类型定义的? - The final local variable cannot be assigned, since it is defined in an enclosing type? 无法分配最终的局部变量标记,因为它是在封闭类型中定义的 - The final local variable token cannot be assigned, since it is defined in an enclosing type 最终局部变量无法分配,因为它是在封闭类型java中定义的 - The final local variable cannot be assigned, since it is defined in an enclosing type java 最终的局部变量 dc 无法赋值,因为它是在封闭类型中定义的 - The final local variable dc cannot be assigned, since it is defined in an enclosing type 问题-无法分配最终的局部变量od,因为它是用封闭类型定义的 - Issue - The final local variable od cannot be assigned, since it is defined in an enclosing type 最终局部变量无法分配,因为它是在封闭类型中定义的 - The final local variable cannot be assigned, since it is defined in an enclosed type 绕过“以封闭类型定义的最终局部变量” - Bypassing “final local variable defined in an enclosing type” 最终变量不能以封闭类型分配 - Final variables cannot be assigned in an enclosing type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM