简体   繁体   English

无法分配最终的局部变量

[英]The final local variable cannot be assigned

I have an array of seats, and the array has two strings(selected and empty). 我有一个座位数组,该数组有两个字符串(已选择和为空)。 On mouse click, I want to traverse the array and find the selected seat. 单击鼠标后,我想遍历数组并找到所选的座位。 When I press the button it says: 当我按下按钮时,它说:

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

    JButton btnContinue = new JButton("Next");
    btnContinue.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent arg0) {

            for(int x=0;x<17;x++){
                if(anArray[x]=="selected"){

                    seatno = anArray[x];
                }
            }

            data page=new data(newfrom,newto,newtime,date2,seatno);
            page.setVisible(true);
            setVisible(false);
        }
    });
    btnContinue.setBounds(358, 227, 62, 23);
    contentPane.add(btnContinue);

The point is that method-local variables from the enclosing type are actually copied to instances of anonymous classes (this is because of activation frame issues, but I won't go further into detail as this is not really relevant to the question), which is why they need to be final, because the variable in the nested type instance is not the same anymore. 关键是实际上封闭类型中的方法局部变量复制到匿名类的实例中(这是由于激活框架的问题,但我将不做进一步的详细介绍,因为这与问题无关)。这就是为什么它们需要是final的原因,因为嵌套类型实例中的变量不再相同。

So, here is the first example: 因此,这是第一个示例:

void foo() {
    int a = 3;
    new Runnable() {
        @Override
        public void run() {
            a += 3;
        }
    };
}

This does not compile, because you cannot reference a non-final variable in an anonymous class' method. 这不会编译,因为您无法在匿名类的方法中引用非最终变量。 When you add a final modifier to the declaration of a , the value of a would be copied into the created instance of the anonymous class you have defined. 当您添加final修饰符来声明a ,值了a会被复制到您所定义的匿名类的创建的实例。 However, you will not be allowed to change the value of a , because the changes would not be visible to the method where a was declared. 然而,你将不会被允许改变的值a ,因为变化将是不可见的,其中方法a被宣布。

However, anonymous classes are not static, that is, they have a reference to the enclosing instance (unless the method where they are declared is static) which you can use to modify variables of the enclosing instance: 但是,匿名类不是静态的,也就是说,它们具有对封闭实例的引用(除非声明它们的方法是静态的),您可以使用它来修改封闭实例的变量:

int a = 3;

void foo() {
    new Runnable() {
        @Override
        public void run() {
            a += 3;
        }
    };
}

This example does compile and it would increase a by 3 every time the run() method of the anonymous class' instance is called. 这个例子确实编译,它会增加a每次时间3 run()匿名类的实例的方法被调用。 (In this example it is never called, but it is just an example.) (在此示例中,从不调用它,而只是一个示例。)

So, to summarize, you need to convert the variable seatno from a method-local variable to an instance variable of the enclosing type. 因此,总而言之,您需要将变量seatno从方法局部变量转换为封闭类型的实例变量。 Or, if it is yet, you need to remove the final modifier as final variables can only be assigned once. 或者,如果还没有,则需要删除final修饰符,因为final变量只能分配一次。

Update: In Java 8, the concept of effectively final variables is introduced (see Java Language Specification ). 更新:在Java 8中,引入了有效的最终变量的概念(请参阅Java语言规范 )。 However, in the first example of this post, the variable a is assigned multiple times, which prevents it from being effectively final. 但是,在本文的第一个示例中,变量a被分配了多次,从而使变量a无法有效地最终确定。 This means that this example still does not compile with Java 8. (The compile error is "Local variable a defined in an enclosing scope must be final or effectively final") 这意味着该示例仍无法使用Java 8进行编译。(编译错误为“在封闭范围内定义的局部变量a必须是最终的或实际上是最终的”)

A final variable cannot change it's value (it's similar to const from C/C++). 最终变量不能更改其值(类似于C / C ++中的const)。

You probably want to make it a field in a class (without the final keyword of course), not a local variable inside a function. 您可能希望使其成为类中的字段(当然没有final关键字),而不是函数中的局部变量。

Instead of defining a class member variable you can also use a mutable int to achieve the same. 除了定义类成员变量,您还可以使用可变的int实现相同的功能。

void foo() {
    final MutableInt a = new MutableInt(3);
    new Runnable() {
        @Override
        public void run() {
           a.add(3);
        }
    };
}

Since MutableInt is not primitive type (hence passed by reference) and can be reassigned this works. 由于MutableInt不是原始类型(因此通过引用传递),因此可以重新分配。

I recently faced similar problem. 我最近遇到了类似的问题。 In my case it was easier to create final array (or collection) and to add variable, that I wanted to change inside anonymous class, to this array, as below. 在我的情况下,创建最终数组(或集合)并向匿名数组添加变量(这是我想在匿名类中进行的更改)更加容易,如下所示。

   int a = 3;
   final int[] array = new int[1];
   array[0] = a;
   new Runnable() {
       @Override
       public void run() {
           array[0] += 3;
       }
   };

Without knowing the declaration of seatno , I'd suggest to introduce a new variable in the mouseClicked() method that is not final and does the same job as seatno currently does, as the variable seems only to be used inside that method. 在不知道seatno声明的seatno ,我建议在mouseClicked()方法中引入一个新变量,该变量不是 final的,并且与seatno当前所做的工作相同,因为该变量似乎仅在该方法内部使用。

By the way: Capitalize your class names ( data should be Data ). 顺便说一句:大写您的类名( data应为Data )。 Will look much more clear. 看起来会更加清晰。

Make sure your variable doesn't have the final modifier. 确保您的变量没有final修饰符。

//final, can be set only when the object is created.
private final String seatno;

//no final modifier, the value can be set every time you "want"
private String seatno;

Also, to compare Strings you should use equals : 另外,要比较字符串,您应该使用equals

if(anArray[x].equals("selected"))

暂无
暂无

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

相关问题 局部变量不能赋值为final - local variable cannot be assigned as final 最终的局部变量不能分配,不能分配给非最终变量 - The final local variable cannot be assigned , cannot to the non-final variable 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? 最终局部变量无法分配,因为它是在封闭类型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 无法分配最终的局部变量,因为它是在封闭类型中定义的 - 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 最终局部变量无法分配,因为它是在封闭类型中定义的 - The final local variable cannot be assigned, since it is defined in an enclosed type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM