-1

Maybe my question will be so much silly for 2016 because there exist many similar questions but it is very hard to me to understand when should i use final variable inside a method instead of local variable. Which one will be more efficiency and when should i use it.

 public void setmethod(int var)
 {
       final int variable = var;
       System.out.println(variable);
 }

public void setmethod(int var)
 {
       int variable = var;
       System.out.println(variable);
 }

I know that The result in the Output will be the same and also that Final fields - is making that same fields reference immutable but which one option is more efficiency under certain circumstances and Why?? I am a bit confused

Panagiotis Drakatos
  • 2,851
  • 4
  • 31
  • 43
  • you are not using `variable`.. you are calling the `var` parameter..there is no difference – Luminous_Dev Dec 13 '16 at 02:15
  • 1
    Both variables are local. – shmosel Dec 13 '16 at 02:16
  • `Final` means that the variable value will not change. It has the same meaning in a given scope - for an object or method. – MaxZoom Dec 13 '16 at 02:19
  • 1
    you said "instead of a local variable". it's still a local variable, just one which you have declared as final. so from the point of view of "efficiency" etc, there is no difference. the only thing you are doing with final in this case is ensuring you don't accidentally re-assign the value later in the method. which is something you might want / need to do, but usually not. – slipperyseal Dec 13 '16 at 02:19

2 Answers2

3

Several things here:

At this point, Java compilers are sophisticated enough that if there is any performance boost, it is imperceptible unless you are in the tightest loop imaginable.

Before you even consider this optimization, you should be looking throughout your code for other, more macro optimizations (eg- reducing http requests, database requests, etc). I can guarantee that you will get more bang looking for those things, instead quibbling over keywords.

Second, I think you meant this:

public void setmethod(final int var)
{
   System.out.println(var);
}

Third, I would recommend that you always use a final keyword unless you really can't. This makes the intent of the code clearer to anyone reading. It states a contract about what can and cannot happen in your code.

Making your variables and fields final whenever possible just makes good sense from a clarity point of view. It has nothing to do with performance.

I have always thought that this was a design flaw of Java that variables are mutable by default. Rather, by default variables should be final, and Java should have introduced a mutable keyword.

Andrew Eisenberg
  • 28,387
  • 9
  • 92
  • 148
  • i reckon the JIT is going to know the value is immutable even if final isn't used, by virtue of it not actually being reassigned by the code (assuming it doesnt). so final would only be of benefit for compile time safety – slipperyseal Dec 13 '16 at 02:23
  • 1
    I think you're right. I just don't know for certain and didn't want to assert it without evidence. – Andrew Eisenberg Dec 13 '16 at 02:23
1

If you are sure that the variable value should not be changed, then the use of final is a good practice. Assuming you have a complex method, this will cause a compilation error if you accidentally try to change the value of the final variable.

Fuxiang Chen
  • 23
  • 1
  • 5