views:

117

answers:

3

I've encountered the code similar to this:

public void foo(String param1) { 
    final String param1F = param1;
    ...
}

I doubt that the author doesn't know that he can put final keyword directly in the method signature cause in the rest of the method he only uses param1F but I'm curious if anyone has the idea for what this could be useful?

A: 

In this case, you could reassign param1, which wouldn't be possible, if param1 was final.

So there is a slight difference. But to me it is not useful, just because I do not change method parameters in general.

Andreas_D
Ok I thought so at the beginning so I've checked the rest of the code for different usage of param1 and param1F but there wasn't anything like this and then I wanted to see other people's opinion in case I missed something :)
draganstankovic
So maybe it's just a question of (personal) coding style. Sometime people use some patterns just to increase readbility or to follow some (strange) companies coding rules...
Andreas_D
I agree (in fact this could be the second most probable reason), but given the circumstances about the project I observed I will conclude that authors didn't know that they can put final keyword at method signature :)
draganstankovic
A: 

maybe it's just me, but I feel weird about writing to method parameters, or declaring them final. I think they should be final by default. they should not be "variable"

irreputable
Then you should declare them final, because then that would bring reality in line with how you think things "should" be.
Chuck
its not just you, its all the fascists from checkstyle that are trying to force it into everybody. the cool thing is that they will never be final and you have to live with it just like we have to live with stupid rules like this one. plus if you dont understand how java works even if you make them singleton it wont help you.
01
+1  A: 

This is required if you need to access the variable from an anonymous class, eg.:

Runnable f(int i) {
    final int i2 = i;
    return new Runnable() {
        public void run() {
            System.out.println(i2);
        }
    };
}
agsamek
not exactly - this would work too if `i` was final and used in the anonymous class.
Andreas_D
@Andreas_D - sure. Either i need to be final or reassigned to a final variable.
agsamek
the question actually was why reassigning when you can say: f(final int i) { ....
draganstankovic