6

Possible Duplicate:
Is there any performance reason to declare method parameters final in Java?
Why would one mark local variables and method parameters as “final” in Java?

I am using PMD to see the code violations.

Inside a webService Method, I have this below code

public ServiceRequest getData()
{
Status status = new Status();
// code
}

What PMD is suggesting me is that, this local variable status could be declared as final.

My question is, making it final would result in any performance improvements or if not what benefits the code could get?

Community
  • 1
  • 1
  • Duplicate of http://stackoverflow.com/a/266981/259576 – Sandro Munda Feb 08 '12 at 14:59
  • As described in [stackoverflow answers](http://stackoverflow.com/questions/316352/why-would-one-mark-local-variables-and-method-parameters-as-final-in-java) the compiler can produce optimized code for a better performance. – ChangeRequest Feb 08 '12 at 15:04
  • Please describe a *single* optimization that is only possible if a local variable is declared final - because I certainly can't think of any and I can't see one described in your link either. – Voo Feb 08 '12 at 16:01
  • It was analyzed some time ago in this threads: http://stackoverflow.com/questions/306862/does-using-final-for-variables-in-java-improve-garbage-collection and http://stackoverflow.com/questions/266806/is-there-any-performance-reason-to-declare-method-parameters-final-in-java ; also, this one leads to think that this thread could be closed anytime http://stackoverflow.com/questions/316352/why-would-one-mark-local-variables-and-method-parameters-as-final-in-java. Next time, please use search – Alfabravo Feb 08 '12 at 17:17
  • It's not duplicate!!! Other questions are about method parameters not local variables. Please, read carefully what you are commenting! – Przemysław Różycki Mar 19 '15 at 10:20

3 Answers3

4

Taken from the following article: http://www.javapractices.com/topic/TopicAction.do?Id=23

  • clearly communicates your intent
  • allows the compiler and virtual machine to perform minor optimizations
  • clearly flags items which are simpler in behaviour - final says, "If you are looking for complexity, you won't find it here."

This is also discussed in this question: Can excessive use of final hurt more than do good?

Community
  • 1
  • 1
Nick Garvey
  • 2,980
  • 24
  • 31
  • 1
    `allows the compiler and virtual machine to perform minor optimizations` - umn no, not really. What kind of optimization would that be? – Voo Feb 08 '12 at 15:41
  • 2
    I have seen Sun's compiler emit marginally shorter bytecode when the only difference between the two methods has been the "finality" of local variables. Micro-optimizations are a real thing, and compilers do actually make them. What really matters, of course, is what the JIT does with the bytecode, and local references lose their "finality" when compiled to bytecode. I think this is why there is so much speculation about final local variables: it's quite non-deterministic what the results are. However, using final locals can affect the bytecode -- for what it's worth. – Christopher Schultz Aug 09 '13 at 16:32
  • The javac generated bytecode does not encode that a variable is final. The JIT has no idea that the variable was once final. – Steve Kuo May 21 '14 at 16:23
4

final indicates the local variable won't be changed. My feeling is that methods should be so short you should be able to easily understand them and so making the variable final may be a bit redundant.

I prefer to make fields final because making the whole class so short, is a serious limitation. Also fields can have thread safety issues which local variables do not.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • I agree. The only reason I use final locals is when using anonymous classes (but then also not because I **want** to). And I doubt the claimed performance benefits - I don't see what additional information we gain there. – Voo Feb 08 '12 at 16:04
  • The JIT is smart enough to work out the variable is effectively final, so I doubt it improves performance much. The `javac` compiler should also be smart enough to work out a variable is effectively final (i.e. never changed) and not require it for anonymous classes IMHO. `final` variables are useful in large complex methods, but the real solution is to re-factor your code so you don't have large complex methods. ;) – Peter Lawrey Feb 08 '12 at 16:11
  • 1
    Since we don't have references in java I don't see how final changes the code in any way. Consider: `int x = 10`, x will be 10 as long as we don't assign x a new value *inside the method itself* (ok yeah the compiler/JIT has to check whether we don't do that, but that's obvious to check). Yeah final could be useful for other programmers, but then we all agree that if a method is that long and complicated that we don't see all variable assignments at one glance we have a much bigger problem there ;) – Voo Feb 08 '12 at 16:22
  • final local variables (and method parameters) have no performance impact. They aren't even in the bytecode – Steve Kuo May 21 '14 at 16:19
  • @SteveKuo Unless you have debugging turned on in which case they still make no difference. – Peter Lawrey May 21 '14 at 16:29
1

I dont know about performance-benefits by making status final, but PMD is suggesting you this, because probably you are never writing on status after its first initialization.

So what you gain by making it final is just that your code is less error-prone - if you declare it final, you cant overwrite it by mistake...

quaylar
  • 2,617
  • 1
  • 17
  • 31