What is the advantage of declaring a final variable in a for loop?
Not so much in a small piece of code, but if this helps to avoid changing the link during the loop.
For instance:
for( String s : values ) {
computeSomething( s );
... a dozen of lines here...
s = s.trim();
... another dozen of line
otherComputation( s );
}
final, anotherComputation , , , , , .
5 - 15 , , , .
final .
...
for( final String s : values ) {
s = new StringBuilder( s ).reverse().toString();
}
variable s might already have been assigned
, :
for( final String s : value ) {
doSomething( new InnerClass() {
void something() {
s.length();
}
});
}