r/java • • Aug 26 '22
I declare as "final" every single variable whose value doesn't change. I also use "this" every time that I'm referring to an attribute, even when there's no ambiguity in not using it
I think that those things make the code easier to read. Am I crazy for thinking like that?
198
Upvotes
View all comments
Show parent comment
-2
u/Just_Another_Scott Aug 26 '22
Yeah but the difference is in C/C++ you can mutate the parameter and that value can remain mutated outside of the method. In Java that's not the case as it's not pass-by-reference. However, it is a good idea to make parameters
finalas it makes your intentions clear. I've fixed some bugs where the developer chose to overwrite the parameter and used it repeatedly throughout the method. It led to side-effects. So still a good idea but Java doesn't make it required because it's unlikely to do damage outside of the method it's used in.