Local final variable or member?
Hello!Hope to get some comment about this.
I use PMD to check my code as often as I can.
Consider this method:
public boolean isDir() {
File file = new File(filePath);
return (file.isDirectory());
}
PMD gives this comment:
Local variable 'file' could be declared final
If I do that like:
public boolean isDir() {
final File file = new File(filePath);
return (file.isDirectory());
}
Then I get this comment from PMD:
Avoid using final local variables, turn them into fields
Seems like there is a contradiction in PMD?
In this particularly case there is no use for making "file" into a field.
So is there any case when I should set local variables final?
What do you guys prefer?
Best regards
Fredrik
