2

In this answer it is states that you should never change parameters you assign and it would be best if Java would enforce parameters to always be final. I personally agree and never do this. If I want to change something to the parameter and then return it again, I create a local copy in the method, change things about that copy and then return that copy.

This made me wonder, should I make all parameters in my Android/Java project final, or is there a case where I wouldn't want to do this?

Some questions that popped up in my head:

  • Does making a @Override method's parameters final make a different?
  • And as a follow-up to the previous question: Does it matter if I call super(my_final_parameter);?
  • Again a follow-up: With the super(my_final_parameter); of default Java/Android extends, is there any case behind the scenes where these Java/Android classes use the parameter so I can't have them final?

If it doesn't matter for the cases above (or other cases you might think of) if the parameters are final or not, is it a good idea to make every parameter of methods / constructors / overridden methods final?

Community
  • 1
  • 1
Kevin Cruijssen
  • 8,426
  • 8
  • 49
  • 114
  • There actually are cases where you would like to change the parameters. For instance, if you would like to "return" multiple arguments, you could send in multiple parameters, change them in your method and then when the method exits you know that the variables have been "returned"/changed/updated. I know that the example is maybe not desirable code, but I do believe that making all your parameters final can give you some headache bugs. This is of course depending on project size and implementation, but as a general idea I wouldnt make all of them final. – Pphoenix Jun 27 '14 at 14:25
  • @Pphoenix since java is pass-by-value modifying your parameters will have no effect on your passed variables, thus _send in multiple parameters, change them in your method and then when the method exits you know that the variables have been "returned"/changed/updated_ is wrong. – Narmer Jun 27 '14 at 15:11

1 Answers1

1

Since Java is pass-by-value and not pass-by-reference, modifying your parameters would have no effect on the original variable.

Adding final to your parameters avoids to whoever has to modify your code (also you) to run in this "bug". So it's up to you, if you want to be sure to not trying to modify your parameter thinking it will modify your variable, use final.

I personally don't use this practice since I'm aware of this behavior (and my first language is java).

As for your popped out questions they asnwer quite by themselves having understood the above. An overriding method simply is a different implementation of a super method, the super parameters have nothing to do with its runtime behavior, since the overriding method is chosen. Also calling super(final_var), as java is pass-by-value, simply passes the value to the super class, leaving final_var untouched and thus not violating the final modifier.

Community
  • 1
  • 1
Narmer
  • 1,386
  • 6
  • 16
  • That's what I meant with your last statement. If you pass a final-var in a super-method, and in that extended class (behind the scenes) they would modify the parameter in the method, then it would cause errors because of the final-var in the super. Anyway, since it's both better for readability and when other people would in the future continue with my code, I just leave all my parameters as non-final, except in cases like: [passing a parameter to a different Thread](http://stackoverflow.com/a/2236657/1682559). I just came across the SO-question and was wondering if it would make a different. – Kevin Cruijssen Jun 30 '14 at 07:28