Answer by Doug Bell:
A few of the answers got this correct, but I'll answer more directly.
The only reason there arefinal
variables in Java is to allow local variables to be referenced from inner classes scoped within the method. Final variables were not added because of any consideration that declaring local variables asfinal
was particularly useful.
The reason that a variable referenced from an inner class needs to be declaredfinal
is because the object(s) instantiated from the inner class receive only the value of the variable at instantiation. The inner class instances do not have any reference to the variable.
Even if it were possible in Java to have a "pointer" to point to the primitive local stack variable, the lifetime of the instantiated object is often longer than the lifetime of the stack frame containing the value.