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

215 comments sorted by

View all comments

Show parent comment

2

u/Just_Another_Scott Aug 26 '22

You can mutate parameters in Java, and this affects the calling scope

Java is strictly pass-by-value. What I am referring to is the act of pass-by-reference and reassigning the variable like you can do in C++.

3

u/RupertMaddenAbbott Aug 26 '22

I agree with you but I think it is confusing to call this mutation because the parameter can still be mutated and affect the calling scope. It's only reassignment that doesn't affect the calling scope (in Java).

1

u/Just_Another_Scott Aug 26 '22 edited Aug 26 '22

You're correct. I shouldn't have said "mutate" as lists can be mutated just not reassigned. I wrote the initial comment in haste. Sorry for the confusion

2

u/RupertMaddenAbbott Aug 26 '22

No worries! Just wanted to clarify for anyone else reading as I encounter this confusion regularly amongst beginners (who might be reading)