Should I make all parameters in Java/Android final? -


in this answer states should never change parameters assign , best if java enforce parameters final. agree , never this. if want change parameter , return again, create local copy in method, change things copy , return copy.

this made me wonder, should make parameters in android/java project final, or there case wouldn't want this?

some questions popped in head:

  • does making @override method's parameters final make different?
  • and follow-up previous question: matter if call super(my_final_parameter);?
  • again follow-up: super(my_final_parameter); of default java/android extends, there case behind scenes these java/android classes use parameter can't have them final?

if doesn't matter cases above (or other cases might think of) if parameters final or not, idea make every parameter of methods / constructors / overridden methods final?

since java pass-by-value , not pass-by-reference, modifying parameters have no effect on original variable.

adding final parameters avoids whoever has modify code (also you) run in "bug". it's you, if want sure not trying modify parameter thinking modify variable, use final.

i don't use practice since i'm aware of behavior (and first language java).

as popped out questions asnwer quite having understood above. overriding method different implementation of super method, super parameters have nothing runtime behavior, since overriding method chosen. calling super(final_var), java pass-by-value, passes value super class, leaving final_var untouched , not violating final modifier.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -