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.

App Kart
  • 924
  • 2
  • 13
  • 24
  • 3
    possible duplicate of [Declaring parameter of a function as final: why and when is it needed?](http://stackoverflow.com/questions/8187859/declaring-parameter-of-a-function-as-final-why-and-when-is-it-needed) – assylias Mar 28 '12 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 . http://stackoverflow.com/questions/40480/is-java-pass-by-reference – kosa Mar 28 '12 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). – Voo Mar 28 '12 at 15:21
  • @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. – kosa Mar 28 '12 at 15:26
  • @thinksteep Yeah I've programmed Java for more than a day, so yes I do know that Java's pass by value. I think if someone doesn't understand one of the most basic principles of the language, throwing final around everywhere won't help them much in the longer run (ie final doesn't give them any additional guarantees, it just doesn't allow them to do something that does something quite different than what they expect) – Voo Mar 28 '12 at 15:29
  • until now i did not find exact answer? – App Kart Mar 28 '12 at 15:33
  • @Voo: well, I added this as comment because I don't know what is OP expertise and I know lot of people mis-understood this concept. My apologies if this is irrelevant. I do agree over engineering is bad. – kosa Mar 28 '12 at 15:36
  • @ArunKumar: I couldn't find exact references, but some where I read that defining final has some optimization advantages. I guess that may be reason why people opt for defining it as final. I may be wrong too. – kosa Mar 28 '12 at 15:38
  • @thinksteep: & @@Voo Just read and apply .its fine but why do it, most important.Thanks – App Kart Mar 28 '12 at 15:42
  • @Voo:"final doesn't give them any additional guarantees, it just doesn't allow them to do something that does something quite different than what they expect" I feel this is useful feature where you will get some confidence that you are not doing what you do want. With autosuggest features of IDE it is very easy to type in something which you don't want and overlook. – kosa Mar 28 '12 at 15:44
  • @ArunKumar: Refer this, http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.4. refer section 14.4.1, final is optional and if you click 4.12.4, it just says "Declaring a variable final can serve as useful documentation that its value will not change and can help avoid programming errors". So, answer for your question may be "To get help in avoid programming errors". If you think your code took care of "avoid programming errors", you don't need to define it as final. – kosa Mar 28 '12 at 16:02
  • I wrote something about this on my blog: [Why every Java field should have been final by default](http://javarizon.wordpress.com/2010/06/26/why-every-java-field-should-have-been-final-by-default/) – Christophe Roussy Mar 28 '12 at 15:22

3 Answers3

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.

Community
  • 1
  • 1
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • 4
    IMHO it also decreases readability. Not sure if it's sensible to do that for every parameter and local variable. – Niklas B. Mar 28 '12 at 15:05
  • 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 – ControlAltDel Mar 28 '12 at 15:06
  • is it not compulsory local make as final? – App Kart Mar 28 '12 at 15:11
  • which case it is not compulsory ? – App Kart Mar 28 '12 at 15:19
  • 1
    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++. – Voo Mar 28 '12 at 15:22
  • but many java developer do local variable as final.my question why? – App Kart Mar 28 '12 at 15:24
  • I agree with Voo. final parameters just adds clutter. There are also time when you want to modify the parameters. – Steve Kuo Mar 28 '12 at 15:50
  • 1
    @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. – Voo Mar 28 '12 at 16:04
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
Israel Unterman
  • 13,158
  • 4
  • 28
  • 35
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

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80