0

Why local variable make as final whether method argument or inside method.

private void add(final int a , final int b) {
    final int c = 0;
}

Please anyone clarify.i am searching a lot but i did not find exact answer.

12
  • 3
    possible duplicate of Declaring parameter of a function as final: why and when is it needed? Commented Mar 28, 2012 at 15:05
  • See this link, I feel it really saves you in case of objects are passed as parameters and if you use objects in your example instead of primitive types after reading this link, you will see real advantage . stackoverflow.com/questions/40480/is-java-pass-by-reference Commented Mar 28, 2012 at 15:10
  • @thinksteep I'm really not following you there. final has absolutely no consequence when adding it to parameters (well apart from the obvious ones that are true in every context). Commented Mar 28, 2012 at 15:21
  • I wrote something about this on my blog: Why every Java field should have been final by default Commented Mar 28, 2012 at 15:22
  • @Voo: It is very simple. Create class A with one property, set some value to this property and pass it as parameter object to method without final. Inside method first print the value and then set something to it. After method invocation done (means outside of the method) print the parameter value again. You will get what I mean. Then refer the link I posted above. You will understand why it is. Commented Mar 28, 2012 at 15:26

3 Answers 3

6

One reason is it prevents you inadvertently changing them. It's good practise since it'll catch some hard-to-spot coding mistakes.

The second reason is that if you're using inner classes, variables referenced from an outer scope need to be declared as final. See this SO answer for more detail.

The problem with final is that it means different things in different contexts. See this discussion for more info.

Sign up to request clarification or add additional context in comments.

8 Comments

IMHO it also decreases readability. Not sure if it's sensible to do that for every parameter and local variable.
In regards to the second reason, this is because if they are not final, the inner class cannot know if they have subsequently changed or not
is it not compulsory local make as final?
Personally I don't think I've ever had a single bug that would've been avoided if I had made a local variable final. Imho final is an exceedingly weak construct compared to say const in C++.
@Arun Where do you get that it's good practice? It's not part of the Oracle style guide as far as I can see and there's clearly no consensus among programmers - actually as you see several people are quite opposed to it. The advantages and disadvantages (visual clutter, doesn't add much) and when it is actually necessary, have all been named - make up your own mind what you prefer.
|
0

Functional style uses final variables. This is one reason. Another one is closures.

Final variables are the only ones that can be used in closures. So if you want to do something like this:

void myMethod(int val) {
    MyClass cls = new MyClass() {
        @override
        void doAction() {
            callMethod(val);  // use the val argument in the anonymous class - closure!
        }
    };
    useClass(cls);
}

This won't compile, as the compiler requires val to be final. So changing the method signature to

void myMethod(final int val)

will solve the problem. Local final variable will do just as well:

void myMethod(int val) {
    final int val0;
    // now use val0 in the anonymous class

Comments

0

final in these instances simply means that the values cannot be changed. Trying to set another value to any of your final variables will result in a compile-time error

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.