This question already has an answer here:
I often encounter methods which look like the following:
public void foo(final String a, final int[] b, final Object1 c){
}
What happens if this method is called without passing it final parameters. i.e. an Object1 that is later changed (so is not declared as final) can be passed to this method just fine
Consider this implementation of foo():
Because the
Runnable
instance would outlive the method, this wouldn't compile without thefinal
keyword --final
tells the compiler that it's safe to take a copy of the reference (to refer to it later). Thus, it's the reference that's considered final, not the value. In other words: As a caller, you can't mess anything up...The
final
keyword on a method parameter means absolutely nothing to the caller. It also means absolutely nothing to the running program, since its presence or absence doesn't change the bytecode. It only ensures that the compiler will complain if the parameter variable is reassigned within the method. That's all. But that's enough.Some programmers (like me) think that's a very good thing and use
final
on almost every parameter. It makes it easier to understand a long or complex method (though one could argue that long and complex methods should be refactored.) It also shines a spotlight on method parameters that aren't marked withfinal
.If you declare any parameter as final, you cannot change the value of it.
Output: Compile Time Error
For more details, please visit my blog: http://javabyroopam.blogspot.com
@stuXnet, I could make the exact opposite argument. If you pass an object to a function, and you change the properties of the passed object then the caller of the function will see the changed value in its variable. This implies a pass by reference system, not pass by value.
What is confusing is the definition of pass by value or pass by reference in a system where the use of pointers is completely hidden to the end user.
Java is definitely NOT pass by value, as to be such, would mean one could mutate the passed object and the original would be unaffected.
Notice you cannot mutate primitives you can only assign them to variables. So testing a Pass by reference or by value by using primitives is not a test.
What you cannot do in Java that can be done in other languages is to reassign the caller's variables to a new value because there are no pointers in Java, so this makes it confusing.
final means you can't change the value of that variable once it was assigned.
Meanwhile, the use of final for the arguments in those methods means it won't allow the programmer to change their value during the execution of the method. This only means that inside the method the final variables can not be reassigned.
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).