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?