error: cannot assign a value to final variable
Solution 1
count++;
will throw an error. Per Oracle,
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.
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.
Solution 2
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.
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.
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
}
}
Solution 3
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[1];
Solution 4
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.
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Admin
Updated on July 09, 2022Comments
-
Admin almost 2 years
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.
-
Daniel Pryden almost 9 yearsThis is the only answer here that actually references the Java Language Spec. +1
-
Michael over 4 yearssometimes I find that I get this error even when I never initialized the "final" variable when the assignment is in an inner class, and other times it works. Why is that?
-
mbyamukama over 2 years+1 for suggesting this method because, effectively we are not changing "a" as it remains an integer array but its elements are allowed to change