posted 19 years ago
The value of a final variable cannot change, so that value (not the variable) can be integrated into the object as a "constant."
In the example below, x is a local variable that currently holds the value 1. But x is not final, so that value might change later. If a LocalClass object is created, that object might continue to exist after the method has exited. And so if LocalClass had been allowed to use x, then there would be a problem, because x would no longer exist and the object would have no way of knowing whether the value 1 is still valid.
On the other hand, y is also a local variable. But y is final, so its value of 2 cannot change. Now if a LocalClass object is created, it can simply copy the value of y. When the method exits and y is destroyed, that's okay because the object doesn't have to go back to y for its value. The object uses a "hard-coded" value of 2 instead of the variable y.
(With objects, of course, we're talking about references being final. So if refA is final, then refA will always "point" to the same object.)
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org