Java 错误:无法为最终变量赋值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32791995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error: cannot assign a value to final variable
提问by Kathryn
I am working on an assignment and I'm stuck on this error: cannot assign a value to final variable count
我正在处理一项任务,但遇到了这个错误:无法为最终变量计数赋值
Here is my code so far...
到目前为止,这是我的代码...
public class List
{
private final int Max = 25;
private final int count;
private Person list[];
public List()
{
count = 0;
list = new Person[Max];
}
public void addSomeone(Person p)
{
if (count < Max){
count++; // THIS IS WHERE THE ERROR OCCURS
list[count-1] = p;
}
}
public String toString()
{
String report = "";
for (int x=0; x < count; x++)
report += list[x].toString() + "\n";
return report;
}
}
I'm very new to java and am obviously not a computer whiz so please explain the problem/solution in the simplest terms possible. Thank you so much.
我对 Java 很陌生,显然不是计算机高手,所以请尽可能用最简单的术语解释问题/解决方案。非常感谢。
采纳答案by Michael
count++;
will throw an error. Per Oracle,
count++;
会抛出错误。根据甲骨文,
A final variable may only be assigned to once. Declaring a variable final can serve as useful documentation that its value will not change and can help avoid programming errors.
一个 final 变量只能赋值一次。声明一个变量 final 可以作为有用的文档,它的值不会改变,并且可以帮助避免编程错误。
You can follow along with that article here. Looking at your code, it seems that you really don't want count
to be final. You want to be able to change its value throughout the program. The fix would be to remove the final
modifier.
您可以在此处阅读该文章。看看你的代码,你似乎真的不想count
成为最终的。您希望能够在整个程序中更改其值。解决方法是删除final
修饰符。
回答by nbokmans
When you declare a variable final
you basically tell the compiler that this variable is constant and will NOT change. You declared count
final, but you hadn't initialized (set a value) it yet. That's why you were allowed to set it's value in your constructor public List() {}
: final variables can be initialized once, and after that, they can't be modified.
当你声明一个变量时,final
你基本上是告诉编译器这个变量是常量并且不会改变。您声明了count
final,但您还没有对其进行初始化(设置值)。这就是为什么允许您在构造函数中设置它的值的原因public List() {}
:final 变量可以初始化一次,之后就不能修改了。
There are exceptions to this though, if you'd for example created an object with an int value of count, and added a setter, you'd still be able to modify the final object.
不过也有例外,例如,如果您创建了一个 int 值为 count 的对象,并添加了一个 setter,您仍然可以修改最终对象。
Example of that:
例如:
public class ExampleObject {
private int count;
public ExampleObject(int count) {
this.count = count;
}
public void setCount(int count) {
this.count = count;
}
public int getCount() {
return count;
}
}
public class ExampleDemo {
private static final ExampleObject obj = new ExampleObject(25);
public static void main(String[] args) {
obj = new ExampleObject(100); //not allowed: cannot assign a value to final variable
obj.setCount(100); //allowed
}
}
回答by Varun
It is throwing error because you have declared count as a final variable. Final variables are nothing but constants. We cannot change the value of a final variable once it is initialized.
它抛出错误,因为您已将 count 声明为最终变量。最终变量只不过是常量。一旦初始化,我们就不能更改最终变量的值。
回答by Varun
'final' keyword works as constant. You can assign a value then and there at the declartion or you can assign a value in a constructor but that too only once. You cannot inc-dec its value further down the code. Therefore count++ will generate an error.
'final' 关键字用作常量。您可以在声明时立即分配一个值,或者您可以在构造函数中分配一个值,但也只能分配一次。您不能在代码中进一步降低其值。因此 count++ 会产生错误。
Also know that 'final' when used with classes or functions, prevents them from being inherited into the sub class.
还要知道“final”与类或函数一起使用时,会阻止它们被继承到子类中。
回答by behrad
there is a way that you can use an array instead of a variable and work with its first element.
有一种方法可以使用数组而不是变量并使用它的第一个元素。
final int[] a = new int[0];