Alex Rivera | Logout

Is there any performance reason to declare method parameters final in Java?

Asked 2008-11-05T21:38:58.557
122

Is there any performance reason to declare method parameters final in Java?

As in:

public void foo(int bar) { ... }

Versus:

public void foo(final int bar) { ... }

Assuming that bar is only read and never modified in foo().

Edit
Report

1 Answer

15

The only benefit to a final parameter is that it can be used in anonymous nested classes. If a parameter is never changed, the compiler will already detect that as part of its normal operation even without the final modifier. It's pretty rare that bugs are caused by a parameter being unexpectedly assigned - if your methods are big enough to need this level of engineering, make them smaller - methods you call can't change your parameters.

answered 2008-11-05T21:58:00.607

Your Answer