1

Possible Duplicate:
Is there any performance reason to declare method parameters final in Java?
Does use of final keyword in Java improve the performance?

So there's a class-level object variable that's allocated upon object creation and stays put for the lifetime of the object:

class MyClass
{
    private Rect rc = new Rect();
    //...
}

What do I gain be declaring it final? Same question for method-level variables.

Community
  • 1
  • 1
Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281

7 Answers7

3

it is all completely implementation specific.

main reason to use final is to ensure that variable value is not allowed to be change over time. it's a matter of you code logic and not optimization.

aav
  • 2,514
  • 1
  • 19
  • 27
  • 1
    +1: `final` fields are also guaranteed to be visible across thread when a constructor returns. As you say, think correctness rather than performance. Most JVMs are smart enough to work out when a field doesn't get changed. – Peter Lawrey Sep 16 '11 at 15:40
1

Declaring variables final does have benefits that come from immutability. If used properly your code can be more thread safe, provided the variable you're making final does not have internal state that could be changed unexpectedly and effect other threads.

See the other answers here too about JVM optimizations.

James DW
  • 1,815
  • 16
  • 21
1

Final has one obvious use, where it makes the object/variable immutable, now can this feature help in performance gains?

Quoting: The Final Word On the final Keyword, the performance gain you get using final is:

A field doesn't need to be ever reloaded, since its value is guaranteed never to change.

But I see a flaw in the above statement, the value won't be reloaded if it's set only once and final guarantees that it will never be reloaded again, but this does not mean using final directly helps you in performance gain, the performance gain we get here is due to the property that if we make sure we do not set the to something else, it will anyway, won't be reloaded.

So, you answer: Although, it is implementation specific to the JVM and atleast in the HotSpot, you will get no performance gains from using final. Read more: Does use of final keyword in Java improve the performance?

IBM also says you won't get ant performance gain out of final.

Community
  • 1
  • 1
zengr
  • 38,346
  • 37
  • 130
  • 192
0

This might not give you 100% answer. But i have a point which i know.

There is lot of optimization gained by the jvm when you declare variables as final.

I remember a good example where i have if-else conditions in my program and in the condition since i used final variable, complier has evaluted which part of if-else is valid and it stripped out the other lines and i have seen it with javap. I will get you that example soon.

Jayy
  • 2,368
  • 4
  • 24
  • 35
0

It allows for some optimization via memoization. For example, if you have a Rectangle class with final height and final width, then your rectangle's area function only needs to compute it once, since it knows that height and width cannot change.

Michael Holman
  • 901
  • 1
  • 10
  • 26
0

I doubt that final fields have a hughe impact on performance, unless you have shared object in multiple threads. However declaring methods final or even classes will help the jit to determine wether a method can be overridden, or can't. If the jit is certain the method is no where overwritten he might remove the method lookup and use a direct jump (jump subroutine). Also Jits like to inline small final methods.

Angel O'Sphere
  • 2,642
  • 20
  • 18
-1

Besides the obvious compile-time protection from modifications (*) you do gain some form of optimized access to its value. The compiler may for example replace the references of primitive variables with literal values. The runtime can for example place copies of the its content in each thread's private memory space to avoid accessing main memory. It does depend on each JVM implementation.

For local variables you do again have gains, both at compile-time and at run-time, mostly in the form of thread-access optimizations. Bear in mind though, that you will only ever notice these gains in hot spots of your code, code that executes hundreds or thousands of times per second.

(*) final only protects from compile-time modifications after the introduction of Accessible Objects.

vagelis
  • 334
  • 3
  • 9
  • For local variables, finally is only present at compile time so it cannot provide any runtime optimizations. http://stackoverflow.com/questions/266806/is-there-any-performance-reason-to-declare-method-parameters-final-in-java/266981#266981 – Robin Sep 16 '11 at 15:49
  • Are you serious?? You marked my answer not-useful for a "both at runtime"?? What about the rest of my answer, is it not helpful?? What is this a f*cking competition for he who has IT longer?? Wouldn't the comment alone suffice to correct my wrong? Jesus... – vagelis Sep 16 '11 at 15:59