Can final variables be assigned to another variable?
final int a = 10;
Can I later assign that final varaible to another variable like:
int b = a;
Seems like java doesn’t like it when I do that, but from what I understand final variable can only be assigned once. However in my case I am not assigning variable a anything instead I am assigning variable b the value of a.
Can someone englighten me about variable that are final? I’ve researched it on various websites but can’t understand what was going on.
Yes, and yes. Final means the reference can’t change; the value of the referenced object can. Essentially, it is a modifier that applies to the variable name, not to the object itself. When you declare
final Foo a = new Foo();you are saying thatawill always refer to the same object. This is legal:This is pretty much the only thing that isn’t: