When Should One Use Final for Method Parameters and Local Variables

When should one use final for method parameters and local variables?

Obsess over:

  • Final fields - Marking fields as final forces them to be set by end of construction, making that field reference immutable. This allows safe publication of fields and can avoid the need for synchronization on later reads. (Note that for an object reference, only the field reference is immutable - things that object reference refers to can still change and that affects the immutability.)
  • Final static fields - Although I use enums now for many of the cases where I used to use static final fields.

Consider but use judiciously:

  • Final classes - Framework/API design is the only case where I consider it.
  • Final methods - Basically same as final classes. If you're using template method patterns like crazy and marking stuff final, you're probably relying too much on inheritance and not enough on delegation.

Ignore unless feeling anal:

  • Method parameters and local variables - I RARELY do this largely because I'm lazy and I find it clutters the code. I will fully admit that marking parameters and local variables that I'm not going to modify is "righter". I wish it was the default. But it isn't and I find the code more difficult to understand with finals all over. If I'm in someone else's code, I'm not going to pull them out but if I'm writing new code I won't put them in. One exception is the case where you have to mark something final so you can access it from within an anonymous inner class.

  • Edit: note that one use case where final local variables are actually very useful as mentioned by @adam-gent is when value gets assigned to the var in the if/else branches.

Why would one mark local variables and method parameters as final in Java?

You should try to do this, whenever it is appropriate. Besides serving to warn you when you "accidentally" try to modify a value, it provides information to the compiler that can lead to better optimization of the class file. This is one of the points in the book, "Hardcore Java" by Robert Simmons, Jr. In fact, the book spends all of its second chapter on the use of final to promote optimizations and prevent logic errors. Static analysis tools such as PMD and the built-in SA of Eclipse flag these sorts of cases for this reason.

When should i use final variable instead of local variable inside a method

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.

Why Final is used in method parameters

final is used in method parameters to make the references unchangeable after it is passed into the method. This is a specialized way of securing the passed parameters. so, the method receiving will not be able to re-initialize it with new object or value

Use of final local variables in java

Firstly, the part about variables being "overridden" - final has two very different meanings. For classes and methods, it's about inheritance; for variables it's about being read-only.

There's one important "feature" of final local variables: they can be used in local (typically anonymous) inner classes. Non-final local variables can't be. That's the primary use of final for local variables, in my experience.

public void foo() {
final String x = "hello";
String y = "there";

Runnable runnable = new Runnable() {
@Override public void run() {
System.out.println(x); // This is valid
System.out.println(y); // This is not
}
};
runnable.run();
}

Note that as a matter of style, some people like to use final even when they're not capturing the variable in a local inner class. I'd certainly be comfortable with final being the default, but a different modifier for "non-final", but I find that adding the modifier explicitly everywhere is too distracting.

final keyword in method parameters

Java always makes a copy of parameters before sending them to methods. This means the final doesn't mean any difference for the calling code. This only means that inside the method the variables can not be reassigned.

Note that if you have a final object, you can still change the attributes of the object. This is because objects in Java really are pointers to objects. And only the pointer is copied (and will be final in your method), not the actual object.

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

The final keyword does not appear in the class file for local variables and parameters, thus it cannot impact the runtime performance. It's only use is to clarify the coders intent that the variable not be changed (which many consider dubious reason for its usage), and dealing with anonymous inner classes.

There is a lot of argument over whether the final modifier on the method itself has any performance gain since the methods will be inlined by the optimizing compiler at runtime anyway, regardless of the modifier. In this case it should also only be used to restrict the overriding of the method.

how does it matter for methods to have 'final' parameters in Java?

Basically, the difference is between

public int doThing( int value )
{
value = value*2; // OK
return value;
}

and

public int doThing( final int value )
{
value = value*2; // Not OK
return value;
}

This can be helpful to you as a programmer to prevent you from changing the value accidentally.

There is one situation where the final keyword is necessary, and that is if you want to use the value in anonymous nested classes, e.g:

public Object makeThing( final String name )
{
return new Object()
{
@Override
public String toString(){
return name; // Won't work if `name` is not `final`.
}
};
}

Related:

  • Is there any performance reason to declare method parameters final in Java?
  • When should one use final for method parameters and local variables?
  • Using "final" modifier whenever applicable in java
  • Why would one mark local variables and method parameters as "final" in Java?
  • Why should I use the keyword "final" on a method parameter in Java?


Related Topics



Leave a reply



Submit