<p>It is highly desirable to declare member variables (both static and instance) as <code>final</code> in order to advertise and enforce the statelessness and immutability of a given class. It is, however, <em>not</em> desirable to declare local variables as <code>final</code> for the following reasons:</p>
<ol>
<li>
<p>It is not the idiom in Java to do so. This is not to say that people never do it, but that if you scan the majority of large, popular open source Java codebases, you will find that most local variables are <em>not</em> declared <code>final</code>, and that where they are, it is done in a way inconsistent with the rest of the codebase.</p>
</li>
<li>
<p>There is typically no need to declare local variables as final. There are certain cases, such as when a variable is declared and then subsequently used within an anonymous class, but this is rare, and even more rare since Java 8 and lambda expressions, which transparently handle local variables as "effectively final" without any explicit <code>final</code> modifier necessary.</p>
</li>
<li>
<p>If final is "necessary" because you want or need to prevent mutation, your method is probably too complex. Methods should be simple and typically quite short. There should be no need to "enforce" immutability of a local variable in the context of a method. Its purpose should be obvious, and it should be obvious if overwriting the value of the variable is the wrong thing to do.</p>
</li>
<li>
<p>Applying the <code>final</code> modifier to local variables creates line noise without creating any value. The use of <code>final</code> on local variables is doubly distracting to the reader, (a) because it is that many more characters that the reader needs to parse and (b) because as noted in (1) above, it is non-idiomatic to see, meaning that it catches the reader's eye all that much more, and makes them wonder (needlessly) "why is this variable declared <code>final</code>?", only to eventually realize that it didn't need to be final at all, and that this modifier was just a waste of their time.</p>
</li>
<li>
<p>Java 10 introduces the reserved type name <code>var</code> for <a href="https://dzone.com/articles/finally-java-10-has-var-to-declare-local-variables" rel="nofollow">convenient declaration of local variables</a>, but there is no way to declare a <code>var</code> as final (in Scala, for example, this is done with a distinction between <code>var</code> (mutable) and <code>val</code> (immutable) declarations). This means that in Java 10, it will become even <em>less</em> idiomatic to see <code>final</code> local variables, and that you'd need to really go against the grain to declare them that way.</p>
</li>
</ol>

<p style="font-size:small;-webkit-text-size-adjust:none;color:#666;">—<br />You are receiving this because you are subscribed to this thread.<br />Reply to this email directly, <a href="https://github.com/bisq-network/style/issues/11">view it on GitHub</a>, or <a href="https://github.com/notifications/unsubscribe-auth/AkpZtiMqPPdEo1tfTDQtEhcqZuxJncpsks5t55yOgaJpZM4UcR0G">mute the thread</a>.<img src="https://github.com/notifications/beacon/AkpZtofrCzRWKliOZfPRmf9myqjUrX1Uks5t55yOgaJpZM4UcR0G.gif" height="1" width="1" alt="" /></p>
<script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","potentialAction":{"@type":"ViewAction","target":"https://github.com/bisq-network/style/issues/11","url":"https://github.com/bisq-network/style/issues/11","name":"View Issue"},"description":"View this Issue on GitHub","publisher":{"@type":"Organization","name":"GitHub","url":"https://github.com"}}</script>
<script type="application/json" data-scope="inboxmarkup">{"api_version":"1.0","publisher":{"api_key":"05dde50f1d1a384dd78767c55493e4bb","name":"GitHub"},"entity":{"external_key":"github/bisq-network/style","title":"bisq-network/style","subtitle":"GitHub repository","main_image_url":"https://assets-cdn.github.com/images/email/message_cards/header.png","avatar_image_url":"https://assets-cdn.github.com/images/email/message_cards/avatar.png","action":{"name":"Open in GitHub","url":"https://github.com/bisq-network/style"}},"updates":{"snippets":[{"icon":"DESCRIPTION","message":"Do not declare local variables as final (#11)"}],"action":{"name":"View Issue","url":"https://github.com/bisq-network/style/issues/11"}}}</script>
<script type="application/ld+json">{
"@type": "MessageCard",
"@context": "http://schema.org/extensions",
"hideOriginalBody": "false",
"originator": "37567f93-e2a7-4e2a-ad37-a9160fc62647",
"title": "Do not declare local variables as final (#11)",
"sections": [
{
"text": "",
"activityTitle": "**Chris Beams**",
"activityImage": "https://assets-cdn.github.com/images/email/message_cards/avatar.png",
"activitySubtitle": "@cbeams",
"facts": [
{
"name": "Repository: ",
"value": "bisq-network/style"
},
{
"name": "Issue #: ",
"value": 11
}
]
}
],
"potentialAction": [
{
"name": "Add a comment",
"@type": "ActionCard",
"inputs": [
{
"isMultiLine": true,
"@type": "TextInput",
"id": "IssueComment",
"isRequired": false
}
],
"actions": [
{
"name": "Comment",
"@type": "HttpPOST",
"target": "https://api.github.com",
"body": "{\n\"commandName\": \"IssueComment\",\n\"repositoryFullName\": \"bisq-network/style\",\n\"issueId\": 11,\n\"IssueComment\": \"{{IssueComment.value}}\"\n}"
}
]
},
{
"name": "Close issue",
"@type": "HttpPOST",
"target": "https://api.github.com",
"body": "{\n\"commandName\": \"IssueClose\",\n\"repositoryFullName\": \"bisq-network/style\",\n\"issueId\": 11\n}"
},
{
"targets": [
{
"os": "default",
"uri": "https://github.com/bisq-network/style/issues/11"
}
],
"@type": "OpenUri",
"name": "View on GitHub"
},
{
"name": "Unsubscribe",
"@type": "HttpPOST",
"target": "https://api.github.com",
"body": "{\n\"commandName\": \"MuteNotification\",\n\"threadId\": 342957318\n}"
}
],
"themeColor": "26292E"
}</script>