Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error: cannot assign a value to final variable

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.

like image 912
Kathryn Avatar asked Sep 25 '15 23:09

Kathryn


4 Answers

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.

like image 100
Michael Avatar answered Oct 11 '22 01:10

Michael


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
    }
}
like image 38
nbokmans Avatar answered Oct 11 '22 00:10

nbokmans


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.

like image 39
Varun Avatar answered Oct 11 '22 00:10

Varun


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];
like image 2
behrad Avatar answered Oct 11 '22 00:10

behrad