Timeline for In Java, should I use "final" for parameters and locals even when I don't have to?
Current License: CC BY-SA 3.0
12 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Sep 9, 2022 at 22:21 | comment | added | djangofan | Ok, I will use final. The reason being it communicates my intention for the code even if my code never actually tries to re-assign anything. | |
| Jan 28, 2020 at 21:00 | comment | added | DGoiko | Keep in mind that with reflection you can actually change that reference aswell (but you should not! stackoverflow.com/a/3301720/9465588) | |
| Oct 5, 2018 at 4:11 | comment | added | ericn | Your comparison is biased as @user102008 pointed out. Variable assignment is not the same as its value update | |
| S Aug 31, 2016 at 12:31 | history | suggested | CommunityBot | CC BY-SA 3.0 | changed word "ditto" into "compilation error" to avoid confusion for non-native english speakers |
| Aug 31, 2016 at 8:07 | review | Suggested edits | |||
| S Aug 31, 2016 at 12:31 | |||||
| Aug 26, 2016 at 14:22 | comment | added | Newtopian | +1 for the reference/state pitfall. Note though that marking variables final may be necessary to create closures in Java8's new functionnal aspects | |
| Dec 5, 2015 at 0:12 | comment | added | user102008 | "final doesn't guarantee that you can't change the value/state of a (nonprimitive) variable. Only that you can't reassign the reference to that object once initialized." Nonprimitive = reference (the only types in Java are primitive types and reference types). The value of a variable of reference type is a reference. Therefore, you can't reassign the reference = you can't change the value. | |
| May 29, 2013 at 13:05 | comment | added | oberlies | Wouldn't it be great if we could have final parameters & local variables, and still a short and clean syntax? programmers.stackexchange.com/questions/199783/… | |
| Feb 17, 2011 at 8:30 | vote | accept | Oak | ||
| Feb 16, 2011 at 10:35 | comment | added | Oak | Regarding the edit - I'm aware of the semantics of final, thank you :) but good point about short & clean methods - I guess that if the method is short enough that it's obvious a variable isn't being re-assigned into, there's even less motive for considering the final keyword. | |
| Feb 16, 2011 at 9:41 | history | edited | Péter Török | CC BY-SA 2.5 | added 712 characters in body |
| Feb 16, 2011 at 8:28 | history | answered | Péter Török | CC BY-SA 2.5 |