Vlad Ilie Vlad Ilie - 1 year ago 59
Java Question

Why shouldn't all function arguments be declared final?

Ok, so I understand why we should declare an argument to be final from this question, but I don't understand why we shouldn't...

Since Java always uses pass by value, this means that we can't return a new value through the given argument, we can only overwrite it, and make the argument useless therefore, because we don't use the passed value...

Is the only benefit of non-final method arguments in Java the fact that you don't have to make a local variable of the arguments' type?

P.S. This question was triggered by

's rule of

Answer Source

I can think of only 2 reasons not to make a parameter final:

  1. to save the use of a local variable if you need to overwrite the parameter's value in some edge cases (this is the reason you pointed out)
  2. to save 6 characters per parameter (not a good enough reason IMHO, but I have to agree that's the reason that leads me not to write it most of the time... :°)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download