<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: All variables in Java must be final</title>
	<atom:link href="http://codemonkeyism.com/all-variables-in-java-must-be-final/feed/" rel="self" type="application/rss+xml" />
	<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/</link>
	<description></description>
	<lastBuildDate>Fri, 30 Jul 2010 06:49:07 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Eclipse Tips: Auto generate &#8216;final&#8217; on your variable, field, and parameter &#171; firdau.si</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-304920</link>
		<dc:creator>Eclipse Tips: Auto generate &#8216;final&#8217; on your variable, field, and parameter &#171; firdau.si</dc:creator>
		<pubDate>Wed, 07 Jul 2010 13:16:28 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-304920</guid>
		<description>[...] with the up/down side of the suggestion, if you eager to do that, just do that somewhere else (here for [...]</description>
		<content:encoded><![CDATA[<p>[...] with the up/down side of the suggestion, if you eager to do that, just do that somewhere else (here for [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: berry120</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-296000</link>
		<dc:creator>berry120</dc:creator>
		<pubDate>Wed, 02 Jun 2010 17:24:10 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-296000</guid>
		<description>I&#039;ve been coding in Java for a number of years now - and I have to say that, contrary to a lot of the comments here, I almost completely disagree.

I&#039;ll start off with a positive though; I do think the final keyword is underused in Java when dealing with variables that shouldn&#039;t change. I&#039;ll often define parameters and variables as final, and it&#039;s used a lot in my code as such. If I&#039;m declaring a variable I don&#039;t intend to change, I&#039;ll mark it as final so I get an unavoidable warning if I then change it. Sometimes I&#039;ll remove the final keyword, sometimes I&#039;ll do things a different way; the important thing here is it causes me to think. Was my original logic incorrect when marking the variable as final, or am I reusing a variable I shouldn&#039;t touch?

However, all variables being final?! Why?! Counters and loops are the two classic examples, but I&#039;ve reassigned many variables in code other than these and I make no apologies for doing so (parameters included.) I can safely say I&#039;ve never introduced a bug through doing so.

I see the closure example above that avoids loops, but again - why?! It&#039;s introducing something comparatively complicated that can be achieved in a perfectly logical, reasonable and bug free fashion already.

I also completely agree with Java&#039;s methodology of creating classes non-final (extensible) by default. The whole point and focus of OO is that you design classes to be extensible - if you&#039;re finding this difficult (with the exception of a few cases such as singletons / utility classes) then sorry, but you&#039;re just not designing good OO code. Inheritance is a brilliant idea, it vastly cuts down on code duplication and when used correctly reduces the potential for many bugs. It&#039;s a growing trend that people are designing classes not to be extended, or just randomly marking them as final (C# is adding to this trend unfortunately) - this is bad.

&quot;Functional Programming is a state of mind not a programming language. So people can code FP in Java.&quot;
No no no!!! This, in my mind is the statement that sums it all up. Java is not designed for people to write functional style code, it&#039;s designed for people to write object oriented code. If you come along from an OO viewpoint, it&#039;s fantastic. If you come from a functional viewpoint then of course you&#039;re going to have to bodge your way around language features to get it to behave how you want, it just wasn&#039;t designed for that purpose! It&#039;s a bit like me buying a land rover and claiming that it&#039;s not designed nearly as well as a Ferrari for a smooth race track (or vice versa...)

Playing devil&#039;s advocate, you could argue the same case procedurally that the static keyword should be there by default and you should only put a keyword on a method if its non static. If you&#039;re using Java as a procedural language it&#039;ll help cut down on verbosity, but in short you just shouldn&#039;t be doing that.

If you want to write functional code then by all means go ahead, but Java is not the tool for the job (and neither should it be.) Yes, it&#039;s a mindset, but it&#039;s a mindset that you generally apply to specific languages like Erlang, F# or Haskell - languages designed for that purpose.

&quot;It’s never too late. And Java has shown that it can adapt, very slowly over several years, but it happens. See the very late changes to volatile&quot;
Sorry to say, but I can guarantee you with near 100% certainty that Java will never make variables or classes final by default. The changes to volatile were necessary to fix the broken double checked locking pattern (needed to lazily instantiate singletons in a threadsafe manner.) They were minor changes and actually fixed a heck of a lot of previous code rather than breaking any (IIRC the change was to the optimisation side of the JIT compiler, as such the only side effect was a very minor performance hit on some JVMs which has now been addressed in the vast majority of cases.)
Changing the fundamental behaviour of a major keyword though is a complete no go. Aside from the fact it&#039;d break pretty much ALL existing code, I think you&#039;ll find the vast majority of Java developers will take a similar standpoint to me and disagree with the change entirely. Major disruption over a very controversial change just won&#039;t be allowed - just look at the effort they took in getting generics working in a backwards compatible way for instance (and a very good job they did of it too.)
Yes, I&#039;ve read the comments about the -final switch on the compiler, but in practice this is again a silly idea in my opinion. Do I have to blindly guess at how I should compile each java project, with or without -final? You&#039;re also losing a lot of intelligibility, when reading some Java at the moment I can tell you just based on the code whether it will compile or not. Start adding in all sorts of bizarre switches that change the behaviour of fundamental keywords and I can&#039;t say whether it will or won&#039;t, or how it&#039;ll behave... that takes me back to VB and the likes of whether I&#039;ve got option explicit on or not, which is not a mindset I want to be in!

If you were talking about a functional style language here, I&#039;d entirely agree. But you&#039;re not - you&#039;re talking about adding very functional concepts to an OO language...</description>
		<content:encoded><![CDATA[<p>I&#8217;ve been coding in Java for a number of years now &#8211; and I have to say that, contrary to a lot of the comments here, I almost completely disagree.</p>
<p>I&#8217;ll start off with a positive though; I do think the final keyword is underused in Java when dealing with variables that shouldn&#8217;t change. I&#8217;ll often define parameters and variables as final, and it&#8217;s used a lot in my code as such. If I&#8217;m declaring a variable I don&#8217;t intend to change, I&#8217;ll mark it as final so I get an unavoidable warning if I then change it. Sometimes I&#8217;ll remove the final keyword, sometimes I&#8217;ll do things a different way; the important thing here is it causes me to think. Was my original logic incorrect when marking the variable as final, or am I reusing a variable I shouldn&#8217;t touch?</p>
<p>However, all variables being final?! Why?! Counters and loops are the two classic examples, but I&#8217;ve reassigned many variables in code other than these and I make no apologies for doing so (parameters included.) I can safely say I&#8217;ve never introduced a bug through doing so.</p>
<p>I see the closure example above that avoids loops, but again &#8211; why?! It&#8217;s introducing something comparatively complicated that can be achieved in a perfectly logical, reasonable and bug free fashion already.</p>
<p>I also completely agree with Java&#8217;s methodology of creating classes non-final (extensible) by default. The whole point and focus of OO is that you design classes to be extensible &#8211; if you&#8217;re finding this difficult (with the exception of a few cases such as singletons / utility classes) then sorry, but you&#8217;re just not designing good OO code. Inheritance is a brilliant idea, it vastly cuts down on code duplication and when used correctly reduces the potential for many bugs. It&#8217;s a growing trend that people are designing classes not to be extended, or just randomly marking them as final (C# is adding to this trend unfortunately) &#8211; this is bad.</p>
<p>&#8220;Functional Programming is a state of mind not a programming language. So people can code FP in Java.&#8221;<br />
No no no!!! This, in my mind is the statement that sums it all up. Java is not designed for people to write functional style code, it&#8217;s designed for people to write object oriented code. If you come along from an OO viewpoint, it&#8217;s fantastic. If you come from a functional viewpoint then of course you&#8217;re going to have to bodge your way around language features to get it to behave how you want, it just wasn&#8217;t designed for that purpose! It&#8217;s a bit like me buying a land rover and claiming that it&#8217;s not designed nearly as well as a Ferrari for a smooth race track (or vice versa&#8230;)</p>
<p>Playing devil&#8217;s advocate, you could argue the same case procedurally that the static keyword should be there by default and you should only put a keyword on a method if its non static. If you&#8217;re using Java as a procedural language it&#8217;ll help cut down on verbosity, but in short you just shouldn&#8217;t be doing that.</p>
<p>If you want to write functional code then by all means go ahead, but Java is not the tool for the job (and neither should it be.) Yes, it&#8217;s a mindset, but it&#8217;s a mindset that you generally apply to specific languages like Erlang, F# or Haskell &#8211; languages designed for that purpose.</p>
<p>&#8220;It’s never too late. And Java has shown that it can adapt, very slowly over several years, but it happens. See the very late changes to volatile&#8221;<br />
Sorry to say, but I can guarantee you with near 100% certainty that Java will never make variables or classes final by default. The changes to volatile were necessary to fix the broken double checked locking pattern (needed to lazily instantiate singletons in a threadsafe manner.) They were minor changes and actually fixed a heck of a lot of previous code rather than breaking any (IIRC the change was to the optimisation side of the JIT compiler, as such the only side effect was a very minor performance hit on some JVMs which has now been addressed in the vast majority of cases.)<br />
Changing the fundamental behaviour of a major keyword though is a complete no go. Aside from the fact it&#8217;d break pretty much ALL existing code, I think you&#8217;ll find the vast majority of Java developers will take a similar standpoint to me and disagree with the change entirely. Major disruption over a very controversial change just won&#8217;t be allowed &#8211; just look at the effort they took in getting generics working in a backwards compatible way for instance (and a very good job they did of it too.)<br />
Yes, I&#8217;ve read the comments about the -final switch on the compiler, but in practice this is again a silly idea in my opinion. Do I have to blindly guess at how I should compile each java project, with or without -final? You&#8217;re also losing a lot of intelligibility, when reading some Java at the moment I can tell you just based on the code whether it will compile or not. Start adding in all sorts of bizarre switches that change the behaviour of fundamental keywords and I can&#8217;t say whether it will or won&#8217;t, or how it&#8217;ll behave&#8230; that takes me back to VB and the likes of whether I&#8217;ve got option explicit on or not, which is not a mindset I want to be in!</p>
<p>If you were talking about a functional style language here, I&#8217;d entirely agree. But you&#8217;re not &#8211; you&#8217;re talking about adding very functional concepts to an OO language&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matt Gumbley</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-284184</link>
		<dc:creator>Matt Gumbley</dc:creator>
		<pubDate>Fri, 16 Apr 2010 10:21:37 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-284184</guid>
		<description>@DG &quot;One of my habits is to always assume a multi-threaded environment. So, when I’m working with a static or an instance variable (field) in a method, I’ll assign it to a temporary local variable which won’t be changed by another thread. (Particularly when I’m using lazy instantiation.) Or, if the situation calls for it, I’ll use synchronization.&quot;

When you copy the variable to a temporary, you should do so under synchronization. 

&quot;It is a common mistake to assume that synchronization needs to be used only when writing to shared variables; this is simply not true.&quot; [Java Concurrency in Practice, Goetz et al, p 28]</description>
		<content:encoded><![CDATA[<p>@DG &#8220;One of my habits is to always assume a multi-threaded environment. So, when I’m working with a static or an instance variable (field) in a method, I’ll assign it to a temporary local variable which won’t be changed by another thread. (Particularly when I’m using lazy instantiation.) Or, if the situation calls for it, I’ll use synchronization.&#8221;</p>
<p>When you copy the variable to a temporary, you should do so under synchronization. </p>
<p>&#8220;It is a common mistake to assume that synchronization needs to be used only when writing to shared variables; this is simply not true.&#8221; [Java Concurrency in Practice, Goetz et al, p 28]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-269533</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Fri, 05 Feb 2010 18:41:48 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-269533</guid>
		<description>@Chrisn: Yes I have, see my Scala vs. Clojure posts and I like Clojure a lot. That said, I think Clojure will need to fight an uphill battle to get into mainstream.</description>
		<content:encoded><![CDATA[<p>@Chrisn: Yes I have, see my Scala vs. Clojure posts and I like Clojure a lot. That said, I think Clojure will need to fight an uphill battle to get into mainstream.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: chrisn</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-269528</link>
		<dc:creator>chrisn</dc:creator>
		<pubDate>Fri, 05 Feb 2010 18:16:48 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-269528</guid>
		<description>First off, I agree with the premise of this article completely.  I think your suggestion about variables being final automatically and then transient or mutable only by declaration is a great idea.

&#039;let&#039; statements make this type of programming a lot easier, at least for me.  They allow declaring more variables and clearly show the dependency graph of how one value depends on previous values.

Have you checked out clojure?  It is quite strict about mutability and super fun because you are programming in LISP which is quite beautiful.

For .net, instead of warping C# into some bizarre form I like to use F# in my happy place where I don&#039;t have to worry about other devs bitching about learning a new language.</description>
		<content:encoded><![CDATA[<p>First off, I agree with the premise of this article completely.  I think your suggestion about variables being final automatically and then transient or mutable only by declaration is a great idea.</p>
<p>&#8216;let&#8217; statements make this type of programming a lot easier, at least for me.  They allow declaring more variables and clearly show the dependency graph of how one value depends on previous values.</p>
<p>Have you checked out clojure?  It is quite strict about mutability and super fun because you are programming in LISP which is quite beautiful.</p>
<p>For .net, instead of warping C# into some bizarre form I like to use F# in my happy place where I don&#8217;t have to worry about other devs bitching about learning a new language.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Randolf Richardson</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-247773</link>
		<dc:creator>Randolf Richardson</dc:creator>
		<pubDate>Sun, 11 Oct 2009 19:35:37 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-247773</guid>
		<description>Regarding &quot;hacking javac&quot; -- when you&#039;re doing that, could you enable the &quot;goto&quot; command?  Thanks.

Facetiously yours (smirking),

Randolf.</description>
		<content:encoded><![CDATA[<p>Regarding &#8220;hacking javac&#8221; &#8212; when you&#8217;re doing that, could you enable the &#8220;goto&#8221; command?  Thanks.</p>
<p>Facetiously yours (smirking),</p>
<p>Randolf.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: 李静</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-246478</link>
		<dc:creator>李静</dc:creator>
		<pubDate>Thu, 01 Oct 2009 06:37:37 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-246478</guid>
		<description>I agree with you because of readability.</description>
		<content:encoded><![CDATA[<p>I agree with you because of readability.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: newton</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-243894</link>
		<dc:creator>newton</dc:creator>
		<pubDate>Tue, 15 Sep 2009 14:48:02 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-243894</guid>
		<description>I think you mean &quot;All class member property must be final&quot;,if not,the code &quot;for(final int=0;...&quot; looks funny.</description>
		<content:encoded><![CDATA[<p>I think you mean &#8220;All class member property must be final&#8221;,if not,the code &#8220;for(final int=0;&#8230;&#8221; looks funny.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: abby, the hacker chick blog</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-242590</link>
		<dc:creator>abby, the hacker chick blog</dc:creator>
		<pubDate>Sun, 06 Sep 2009 17:33:48 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-242590</guid>
		<description>I really like this idea and am also trying to apply it to C# (which uses const &amp; readonly) - but I&#039;m not finding a way to make method params &quot;final&quot;.  Any ideas how to duplicate this behavior in C#?</description>
		<content:encoded><![CDATA[<p>I really like this idea and am also trying to apply it to C# (which uses const &amp; readonly) &#8211; but I&#8217;m not finding a way to make method params &#8220;final&#8221;.  Any ideas how to duplicate this behavior in C#?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eclipse Tips: Auto generate &#8216;final&#8217; variable, field, and parameter &#124; satukubik</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-239195</link>
		<dc:creator>Eclipse Tips: Auto generate &#8216;final&#8217; variable, field, and parameter &#124; satukubik</dc:creator>
		<pubDate>Thu, 13 Aug 2009 10:05:52 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-239195</guid>
		<description>[...] with the up/down side of the suggestion, if you eager to do that, just do that somewhere else (here for [...]</description>
		<content:encoded><![CDATA[<p>[...] with the up/down side of the suggestion, if you eager to do that, just do that somewhere else (here for [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: James E. Ervin</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-238899</link>
		<dc:creator>James E. Ervin</dc:creator>
		<pubDate>Tue, 11 Aug 2009 04:08:38 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-238899</guid>
		<description>I agree with your basic premise, I love using final everywhere.  I love using them in classes to make them immutable.  What I wish was the case is that Java and Java Beans would better support them.  I find it so frustrating that to use any bean utility or more importantly do any form of serialization (to XML, Java serialization, or databases) you must have a bloody no arg constructor!  If there were only a good way around that constraint.</description>
		<content:encoded><![CDATA[<p>I agree with your basic premise, I love using final everywhere.  I love using them in classes to make them immutable.  What I wish was the case is that Java and Java Beans would better support them.  I find it so frustrating that to use any bean utility or more importantly do any form of serialization (to XML, Java serialization, or databases) you must have a bloody no arg constructor!  If there were only a good way around that constraint.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: peter royal</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-238707</link>
		<dc:creator>peter royal</dc:creator>
		<pubDate>Mon, 10 Aug 2009 14:05:15 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-238707</guid>
		<description>intellij has an inspection for re-use of local variables, so you have have it flag it as a warning/error as desired without the noise of the final keyword everywhere.</description>
		<content:encoded><![CDATA[<p>intellij has an inspection for re-use of local variables, so you have have it flag it as a warning/error as desired without the noise of the final keyword everywhere.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Itay Maman</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-238631</link>
		<dc:creator>Itay Maman</dc:creator>
		<pubDate>Sun, 09 Aug 2009 18:57:18 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-238631</guid>
		<description>I think that final fields are great. Declaring most/all of the fields as final dramatically reduces the chances of Null-Pointer-Exceptions in the code.

As for local variables things are somewhat more complicated. One case is loop variables which cannot be final. Another case is formal parameters whose values sometimes need to be adjusted. Of course you this can be solved by introducing an additional local variable but it is not always worth the extra verbosity.

Anyway, I agree that final should be the default. Also, I like very much the idea of having the final qualifier hidden by the IDE.</description>
		<content:encoded><![CDATA[<p>I think that final fields are great. Declaring most/all of the fields as final dramatically reduces the chances of Null-Pointer-Exceptions in the code.</p>
<p>As for local variables things are somewhat more complicated. One case is loop variables which cannot be final. Another case is formal parameters whose values sometimes need to be adjusted. Of course you this can be solved by introducing an additional local variable but it is not always worth the extra verbosity.</p>
<p>Anyway, I agree that final should be the default. Also, I like very much the idea of having the final qualifier hidden by the IDE.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter Lawrey</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-224139</link>
		<dc:creator>Peter Lawrey</dc:creator>
		<pubDate>Tue, 06 Jan 2009 08:29:01 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-224139</guid>
		<description>One problem with making class either final or abstract is that you cannot create instance of abstract classes.  This may be too restrictive and I think a lot of classes are neither final or abstract for good reason.
I would suggest in this case there be a third option of an inheritable class which can be instanciated.</description>
		<content:encoded><![CDATA[<p>One problem with making class either final or abstract is that you cannot create instance of abstract classes.  This may be too restrictive and I think a lot of classes are neither final or abstract for good reason.<br />
I would suggest in this case there be a third option of an inheritable class which can be instanciated.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Greg Mildenhall</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-224126</link>
		<dc:creator>Greg Mildenhall</dc:creator>
		<pubDate>Tue, 06 Jan 2009 02:07:20 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-224126</guid>
		<description>&quot;There&#039;s a reason they&#039;re called variables.&quot;

Yes, but it&#039;s not what you think.

It comes from the mathematical usage. The value of the variable varies from one application of the function (invocation of the method) to the next, _not_ within one. So in maths, all variables are final - and their functions end up so clearly-defined and well-behaved that they typically _prove_ that they&#039;re correct. ;)

The last sentence was kinda tongue-in-cheek but I do agree with the make-it-final agenda even Java seems to go out of its way in many places to stop you adhering to good coding practices like that one.</description>
		<content:encoded><![CDATA[<p>&#8220;There&#8217;s a reason they&#8217;re called variables.&#8221;</p>
<p>Yes, but it&#8217;s not what you think.</p>
<p>It comes from the mathematical usage. The value of the variable varies from one application of the function (invocation of the method) to the next, _not_ within one. So in maths, all variables are final &#8211; and their functions end up so clearly-defined and well-behaved that they typically _prove_ that they&#8217;re correct. ;)</p>
<p>The last sentence was kinda tongue-in-cheek but I do agree with the make-it-final agenda even Java seems to go out of its way in many places to stop you adhering to good coding practices like that one.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Apocalisp</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-224120</link>
		<dc:creator>Apocalisp</dc:creator>
		<pubDate>Mon, 05 Jan 2009 21:50:11 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-224120</guid>
		<description>I couldn&#039;t agree more. Make everything final that can be. I&#039;d add to that the final keyword for class declarations. All classes in Java should be declared final unless they are declared abstract.</description>
		<content:encoded><![CDATA[<p>I couldn&#8217;t agree more. Make everything final that can be. I&#8217;d add to that the final keyword for class declarations. All classes in Java should be declared final unless they are declared abstract.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: BA</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-83472</link>
		<dc:creator>BA</dc:creator>
		<pubDate>Fri, 02 May 2008 13:14:47 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-83472</guid>
		<description>&quot;It’s the right thing to do, especially in relatively long methods.&quot;

Can you elaborate on the right thing? Why? And, it&#039;s usually considered bad practice to have long methods so that&#039;s not really a good point, is it?</description>
		<content:encoded><![CDATA[<p>&#8220;It’s the right thing to do, especially in relatively long methods.&#8221;</p>
<p>Can you elaborate on the right thing? Why? And, it&#8217;s usually considered bad practice to have long methods so that&#8217;s not really a good point, is it?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sipowitz</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-83406</link>
		<dc:creator>Sipowitz</dc:creator>
		<pubDate>Fri, 02 May 2008 10:03:12 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-83406</guid>
		<description>I adopted the habit of making all variables final about a year ago, and I don&#039;t regret it. It&#039;s the right thing to do, especially in relatively long methods.</description>
		<content:encoded><![CDATA[<p>I adopted the habit of making all variables final about a year ago, and I don&#8217;t regret it. It&#8217;s the right thing to do, especially in relatively long methods.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: peter lawrey</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-82290</link>
		<dc:creator>peter lawrey</dc:creator>
		<pubDate>Wed, 30 Apr 2008 05:59:13 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-82290</guid>
		<description>&quot;The semantics are the same for the caller of your method whether you make them final or not. &quot;
Good point. I would agree that final parameters for a method make no difference to the caller.  So making them final by default would have no impact in that situation either.</description>
		<content:encoded><![CDATA[<p>&#8220;The semantics are the same for the caller of your method whether you make them final or not. &#8221;<br />
Good point. I would agree that final parameters for a method make no difference to the caller.  So making them final by default would have no impact in that situation either.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-82267</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Wed, 30 Apr 2008 04:49:14 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-82267</guid>
		<description>@hacklberry: As I&#039;ve written lots of Ruby, Perl, Python, C, Delphi and some Groovy in the past 15 years, shouldn&#039;t it be that &lt;i&gt;&quot;the most common charactersitic of Ruby, Perl, Python, C, Delphi, Groovy and Java developers is their ignorance&quot;&lt;/i&gt; ? :-)</description>
		<content:encoded><![CDATA[<p>@hacklberry: As I&#8217;ve written lots of Ruby, Perl, Python, C, Delphi and some Groovy in the past 15 years, shouldn&#8217;t it be that <i>&#8220;the most common charactersitic of Ruby, Perl, Python, C, Delphi, Groovy and Java developers is their ignorance&#8221;</i> ? :-)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: hacklberry</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-82172</link>
		<dc:creator>hacklberry</dc:creator>
		<pubDate>Wed, 30 Apr 2008 00:43:26 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-82172</guid>
		<description>&quot;I haven’t been using the final keyword in Java for 10 years...&quot; - that is sad, was someone stopping you from reading (freely available) books? :-)  http://www.parashift.com/c  -faq-lite/const-correctness.html ,and Bruce Eckel wrote about const correctness in Java in his &quot;Thinking in Java&quot;. Why do I always feel that the most common 
 characteristic of java developers is their ignorance? :-)</description>
		<content:encoded><![CDATA[<p>&#8220;I haven’t been using the final keyword in Java for 10 years&#8230;&#8221; &#8211; that is sad, was someone stopping you from reading (freely available) books? :-)  <a href="http://www.parashift.com/c" rel="nofollow">http://www.parashift.com/c</a>  -faq-lite/const-correctness.html ,and Bruce Eckel wrote about const correctness in Java in his &#8220;Thinking in Java&#8221;. Why do I always feel that the most common<br />
 characteristic of java developers is their ignorance? :-)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gabriel Belingueres</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-82129</link>
		<dc:creator>Gabriel Belingueres</dc:creator>
		<pubDate>Tue, 29 Apr 2008 23:08:36 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-82129</guid>
		<description>I always use the final keyword in my code.

At first it seems that writing final everywhere will clutter the code, but I found it the opposite: When I define a non final variable, I KNOW for sure it will be assigned more than one time (this saves time too.)

Consider also the tricky relationship between final and method overriding, as I discussed here:

http://groups.google.com.ar/group/comp.lang.java.programmer/browse_thread/thread/ef31c6c22307d11b/22bc937b17d51703

Gabriel</description>
		<content:encoded><![CDATA[<p>I always use the final keyword in my code.</p>
<p>At first it seems that writing final everywhere will clutter the code, but I found it the opposite: When I define a non final variable, I KNOW for sure it will be assigned more than one time (this saves time too.)</p>
<p>Consider also the tricky relationship between final and method overriding, as I discussed here:</p>
<p><a href="http://groups.google.com.ar/group/comp.lang.java.programmer/browse_thread/thread/ef31c6c22307d11b/22bc937b17d51703" rel="nofollow">http://groups.google.com.ar/group/comp.lang.java.programmer/browse_thread/thread/ef31c6c22307d11b/22bc937b17d51703</a></p>
<p>Gabriel</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-81995</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Tue, 29 Apr 2008 20:47:57 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-81995</guid>
		<description>&quot;Anyway, I have found what works for me and what works for a team of diverse individuals are not the same.  In a team I tend to prefer defensive approaches which minimize assumptions.&quot;

Same for me.</description>
		<content:encoded><![CDATA[<p>&#8220;Anyway, I have found what works for me and what works for a team of diverse individuals are not the same.  In a team I tend to prefer defensive approaches which minimize assumptions.&#8221;</p>
<p>Same for me.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: peter lawrey</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-81952</link>
		<dc:creator>peter lawrey</dc:creator>
		<pubDate>Tue, 29 Apr 2008 19:54:23 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-81952</guid>
		<description>BTW: I was in an interview recently when they were discussing a language they had invented in house. !! While it had some pluses I could see, I was a bit shocked when they said the libraries had 30 million lines of code.

I have developed a small framework which is just over 30K lines and in a recent rewrite I got rid of 10K...

Anyway, I have found what works for me and what works for a team of diverse individuals are not the same.  In a team I tend to prefer defensive approaches which minimize assumptions.</description>
		<content:encoded><![CDATA[<p>BTW: I was in an interview recently when they were discussing a language they had invented in house. !! While it had some pluses I could see, I was a bit shocked when they said the libraries had 30 million lines of code.</p>
<p>I have developed a small framework which is just over 30K lines and in a recent rewrite I got rid of 10K&#8230;</p>
<p>Anyway, I have found what works for me and what works for a team of diverse individuals are not the same.  In a team I tend to prefer defensive approaches which minimize assumptions.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: BA</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-81945</link>
		<dc:creator>BA</dc:creator>
		<pubDate>Tue, 29 Apr 2008 19:03:28 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-81945</guid>
		<description>Not sure what all this noise is about making all variables final. Seems quite ridiculous to make method parameters final in Java. What is this getting you? The semantics are the same for the caller of your method whether you make them final or not. What exactly are you telling the user of your method with this final keyword?</description>
		<content:encoded><![CDATA[<p>Not sure what all this noise is about making all variables final. Seems quite ridiculous to make method parameters final in Java. What is this getting you? The semantics are the same for the caller of your method whether you make them final or not. What exactly are you telling the user of your method with this final keyword?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-81934</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Tue, 29 Apr 2008 17:52:25 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-81934</guid>
		<description>As a team manager I apply the wait, wait, wait strategy. Hiring people who put &lt;code&gt;System.exit()&lt;/code&gt;  everywhere does not scale. In the end it does cost much more than they are worth, as you&#039;ve said.</description>
		<content:encoded><![CDATA[<p>As a team manager I apply the wait, wait, wait strategy. Hiring people who put <code>System.exit()</code>  everywhere does not scale. In the end it does cost much more than they are worth, as you&#8217;ve said.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: DG</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-81922</link>
		<dc:creator>DG</dc:creator>
		<pubDate>Tue, 29 Apr 2008 16:05:09 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-81922</guid>
		<description>@stephan,

&lt;blockquote&gt;
Wait, wait, wait until you find the right person.
&lt;/blockquote&gt;

Not &lt;i&gt;the&lt;/i&gt; right person, &lt;i&gt;a&lt;/i&gt; right person. Notice that I said &quot;reasonably competent&quot;, not, to use your words, &quot;the best&quot;.

Whoever randomly sprinkles a codebase with unwarned System.exits or endless recursions, is not competent at all. They&#039;re doing more harm to their team&#039;s development effort than good, at least in my opinion. Keeping them on the team is a failure on the part of management, again in my opinion.</description>
		<content:encoded><![CDATA[<p>@stephan,</p>
<blockquote><p>
Wait, wait, wait until you find the right person.
</p></blockquote>
<p>Not <i>the</i> right person, <i>a</i> right person. Notice that I said &#8220;reasonably competent&#8221;, not, to use your words, &#8220;the best&#8221;.</p>
<p>Whoever randomly sprinkles a codebase with unwarned System.exits or endless recursions, is not competent at all. They&#8217;re doing more harm to their team&#8217;s development effort than good, at least in my opinion. Keeping them on the team is a failure on the part of management, again in my opinion.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-81900</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Tue, 29 Apr 2008 15:19:55 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-81900</guid>
		<description>&quot;[...] recruit [...]&quot;

Ah, recruit. Getting the best usually can only be done with:

Lots and lots and lots of money -&gt; need to have a VC or an IPO or both (be Google)

Grow your developers on a tree.

Wait, wait, wait until you find the right person.</description>
		<content:encoded><![CDATA[<p>&#8220;[...] recruit [...]&#8221;</p>
<p>Ah, recruit. Getting the best usually can only be done with:</p>
<p>Lots and lots and lots of money -> need to have a VC or an IPO or both (be Google)</p>
<p>Grow your developers on a tree.</p>
<p>Wait, wait, wait until you find the right person.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: DG</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-81894</link>
		<dc:creator>DG</dc:creator>
		<pubDate>Tue, 29 Apr 2008 15:02:48 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-81894</guid>
		<description>@peter lawrey,

&lt;blockquote&gt;
On one program I worked it had 263 calls to System.exit() typically without a warning message. Numerous examples of infinite recursion.
Often enhancing a feature meant getting it work in the first place.
&lt;/blockquote&gt;

Then I would argue that you&#039;re barking up the wrong tree if you think your problems could be mitigated, much less solved, by coding conventions. Incompetent programmers will produce inferior code, period. It sounds like the root of your problem is your management&#039;s inability to recruit, hire and retain reasonably competent programmers.</description>
		<content:encoded><![CDATA[<p>@peter lawrey,</p>
<blockquote><p>
On one program I worked it had 263 calls to System.exit() typically without a warning message. Numerous examples of infinite recursion.<br />
Often enhancing a feature meant getting it work in the first place.
</p></blockquote>
<p>Then I would argue that you&#8217;re barking up the wrong tree if you think your problems could be mitigated, much less solved, by coding conventions. Incompetent programmers will produce inferior code, period. It sounds like the root of your problem is your management&#8217;s inability to recruit, hire and retain reasonably competent programmers.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-81762</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Tue, 29 Apr 2008 07:27:57 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-81762</guid>
		<description>&quot;However, consider you are in a team of say 80 developers of varying coding standard working on a code base of 2.5 million lines of code which is a few year old&quot;

Ah, exactly my territory :-)</description>
		<content:encoded><![CDATA[<p>&#8220;However, consider you are in a team of say 80 developers of varying coding standard working on a code base of 2.5 million lines of code which is a few year old&#8221;</p>
<p>Ah, exactly my territory :-)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: peter lawrey</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-81757</link>
		<dc:creator>peter lawrey</dc:creator>
		<pubDate>Tue, 29 Apr 2008 07:00:52 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-81757</guid>
		<description>&quot;And then again, maybe I’m just less prone to introducing bugs into my code.&quot;
&quot;I’m not plagued by the sorts of bugs mentioned in the above comments. I occasionally create them, of course, but I find them, fix them and move on.&quot;

I agree that I rarely introduce bugs into code I have written and often fix them quickly when I do.
However, consider you are in a team of say 80 developers of varying coding standard working on a code base of 2.5 million lines of code which is a few year old.  The main application has 600  threads fired off in no consistent manner.

You are reading a large section of code you have to work on for the first time and you have no idea which variables might change when and in what weird and wonderful ways.  The simplest first step is to do what @Torgny Andersson suggest (I use IntelliJ which has 650 code analysis inspections) and turn as many fields final as possible and as many inner classes/methods static as possible etc. Essentially you are doing a basic code tidy up wishing the previous developer had bothered to do more housekeeping.

You might assume why worry if the code works.  Actually this is a dangerous assumption because you will find there have been so many layers of code added that even if the application works the code may have never have worked but someone may have added a work around on top. (Actually several people may have added pieces of work arounds in many different places)  Until the code you are working on isn&#039;t really called so it does really work and never has. 

On one program I worked it had 263 calls to System.exit() typically without a warning message. Numerous examples of infinite recursion.
Often enhancing a feature meant getting it work in the first place.
And in this kind of environment you find lots of bugs which are highlighted by code analysis and a common refactoring trick to get some control over the code is to place final on as many fields as possible.  For large methods it can be worth adding final variables as well.  However I would say a better option is to break up the methods into smaller ones if possible.  When you extract a method in IntelliJ it can detected other blocks of code which can be replaced by the method and gives you the option of replacing these as well.

&quot;I’ll assign it to a temporary local variable&quot; - I assume where ever a variable might be used more than once.
Not sure if this is simpler or not &quot;cluttering up your code&quot; when just adding final does the same thing.  Obviously for non final variables you should do this where appropriate.

&quot;But the discussion above has been more about declaring local variables as final, than fields. &quot;
I take your point. However I would broaden the claim that final be the default modifier for variables and fields and you should have a keyword to signify non-final variables.

&quot;for (final int i = 0; …&quot;
You can use final in foreach loops as the variable has the scope of the block e.g. 

for(final String line: readByLine(filename)) {

}</description>
		<content:encoded><![CDATA[<p>&#8220;And then again, maybe I’m just less prone to introducing bugs into my code.&#8221;<br />
&#8220;I’m not plagued by the sorts of bugs mentioned in the above comments. I occasionally create them, of course, but I find them, fix them and move on.&#8221;</p>
<p>I agree that I rarely introduce bugs into code I have written and often fix them quickly when I do.<br />
However, consider you are in a team of say 80 developers of varying coding standard working on a code base of 2.5 million lines of code which is a few year old.  The main application has 600  threads fired off in no consistent manner.</p>
<p>You are reading a large section of code you have to work on for the first time and you have no idea which variables might change when and in what weird and wonderful ways.  The simplest first step is to do what @Torgny Andersson suggest (I use IntelliJ which has 650 code analysis inspections) and turn as many fields final as possible and as many inner classes/methods static as possible etc. Essentially you are doing a basic code tidy up wishing the previous developer had bothered to do more housekeeping.</p>
<p>You might assume why worry if the code works.  Actually this is a dangerous assumption because you will find there have been so many layers of code added that even if the application works the code may have never have worked but someone may have added a work around on top. (Actually several people may have added pieces of work arounds in many different places)  Until the code you are working on isn&#8217;t really called so it does really work and never has. </p>
<p>On one program I worked it had 263 calls to System.exit() typically without a warning message. Numerous examples of infinite recursion.<br />
Often enhancing a feature meant getting it work in the first place.<br />
And in this kind of environment you find lots of bugs which are highlighted by code analysis and a common refactoring trick to get some control over the code is to place final on as many fields as possible.  For large methods it can be worth adding final variables as well.  However I would say a better option is to break up the methods into smaller ones if possible.  When you extract a method in IntelliJ it can detected other blocks of code which can be replaced by the method and gives you the option of replacing these as well.</p>
<p>&#8220;I’ll assign it to a temporary local variable&#8221; &#8211; I assume where ever a variable might be used more than once.<br />
Not sure if this is simpler or not &#8220;cluttering up your code&#8221; when just adding final does the same thing.  Obviously for non final variables you should do this where appropriate.</p>
<p>&#8220;But the discussion above has been more about declaring local variables as final, than fields. &#8221;<br />
I take your point. However I would broaden the claim that final be the default modifier for variables and fields and you should have a keyword to signify non-final variables.</p>
<p>&#8220;for (final int i = 0; …&#8221;<br />
You can use final in foreach loops as the variable has the scope of the block e.g. </p>
<p>for(final String line: readByLine(filename)) {</p>
<p>}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Torgny Andersson</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-81722</link>
		<dc:creator>Torgny Andersson</dc:creator>
		<pubDate>Tue, 29 Apr 2008 05:18:35 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-81722</guid>
		<description>There is another (more?) important thing about final variables: it communicates the intent of the variable to the reader of the source code.

I find variables that are defined final easier to reason about.

If the varible does/should not change, then make it final. It&#039;s not that hard, is it? Actually, Eclipse can be configured to automatically insert &#039;final&#039; for variables/fields/parameters that can be final. That&#039;s very nice.</description>
		<content:encoded><![CDATA[<p>There is another (more?) important thing about final variables: it communicates the intent of the variable to the reader of the source code.</p>
<p>I find variables that are defined final easier to reason about.</p>
<p>If the varible does/should not change, then make it final. It&#8217;s not that hard, is it? Actually, Eclipse can be configured to automatically insert &#8216;final&#8217; for variables/fields/parameters that can be final. That&#8217;s very nice.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: DG</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-81514</link>
		<dc:creator>DG</dc:creator>
		<pubDate>Mon, 28 Apr 2008 21:46:25 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-81514</guid>
		<description>@peter lawrey:

&lt;blockquote&gt;
&quot;You must have been using Java for very different things than myself.&quot;
&lt;/blockquote&gt;

And then again, maybe I&#039;m just less prone to introducing bugs into my code. I&#039;m not trying to be a smartass when I say that, although I know it sounds that way. If I found myself plagued by careless bugs that I coded all the time, I would probably also be seeking out novel ways to prevent those bugs, even if it meant junking up my code with a lot of defensive measures like declaring every local variable as final. But I&#039;m not plagued by the sorts of bugs mentioned in the above comments. I occasionally create them, of course, but I find them, fix them and move on.

&quot;One of the things final fields help with is thread safety. I can only assume you have never had a field change when you didn’t expect it to.&quot;

No I haven&#039;t. One of my habits is to always assume a multi-threaded environment. So, when I&#039;m working with a static or an instance variable (field) in a method, I&#039;ll assign it to a temporary local variable which won&#039;t be changed by another thread. (Particularly when I&#039;m using lazy instantiation.) Or, if the situation calls for it, I&#039;ll use synchronization. And if the value of a field is never going to change, I have no objection to declaring it as final, and I usually in fact do. 

&quot;There is a measurable benefit in using final fields. Its not big but applications do perform faster as the JVM can optimize access to such fields.&quot;

But the discussion above has been more about declaring local variables as final, than fields. And if there&#039;s any efficiency gained by making a local variable final, it&#039;s likely offset by the cost of extra final variables that are needed every time the value changes (as discussed above), and if not, the gain wouldn&#039;t be worth the extra clutter in the code, at least not to me.</description>
		<content:encoded><![CDATA[<p>@peter lawrey:</p>
<blockquote><p>
&#8220;You must have been using Java for very different things than myself.&#8221;
</p></blockquote>
<p>And then again, maybe I&#8217;m just less prone to introducing bugs into my code. I&#8217;m not trying to be a smartass when I say that, although I know it sounds that way. If I found myself plagued by careless bugs that I coded all the time, I would probably also be seeking out novel ways to prevent those bugs, even if it meant junking up my code with a lot of defensive measures like declaring every local variable as final. But I&#8217;m not plagued by the sorts of bugs mentioned in the above comments. I occasionally create them, of course, but I find them, fix them and move on.</p>
<p>&#8220;One of the things final fields help with is thread safety. I can only assume you have never had a field change when you didn’t expect it to.&#8221;</p>
<p>No I haven&#8217;t. One of my habits is to always assume a multi-threaded environment. So, when I&#8217;m working with a static or an instance variable (field) in a method, I&#8217;ll assign it to a temporary local variable which won&#8217;t be changed by another thread. (Particularly when I&#8217;m using lazy instantiation.) Or, if the situation calls for it, I&#8217;ll use synchronization. And if the value of a field is never going to change, I have no objection to declaring it as final, and I usually in fact do. </p>
<p>&#8220;There is a measurable benefit in using final fields. Its not big but applications do perform faster as the JVM can optimize access to such fields.&#8221;</p>
<p>But the discussion above has been more about declaring local variables as final, than fields. And if there&#8217;s any efficiency gained by making a local variable final, it&#8217;s likely offset by the cost of extra final variables that are needed every time the value changes (as discussed above), and if not, the gain wouldn&#8217;t be worth the extra clutter in the code, at least not to me.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: peter lawrey</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-81496</link>
		<dc:creator>peter lawrey</dc:creator>
		<pubDate>Mon, 28 Apr 2008 21:11:27 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-81496</guid>
		<description>&quot;I can’t recall ever encountering a bug that would have been prevented by adopting this practice.&quot;

You must have been using Java for very different things than myself.  

One of the things final fields help with is thread safety.  I can only assume you have never had a field change when you didn&#039;t expect it to. or you were happy to just synchronize everything, or you only write single threaded code.
I can only assume you have never used a field before it was initialized.
I guess you are lucky that you have only worked with people who always understood all the assumption made about when a field is set and when it can be used.

Or perhaps you haven&#039;t realized how many bugs you could have prevented with final fields.

“Maybe enough programmers will agree with you for it to catch on.”
Unlikely. Most developers don&#039;t like having to think about multi-threaded issues and most applications don&#039;t require them to do so.

However if there were a compiler option I would certainly use it.

There is a measurable benefit in using final fields.  Its not big but applications do perform faster as the JVM can optimize access to such fields.

The biggest benefit is in allowing multi-threaded access without synchronization.  This does provide a significant benefit in highly multi-threaded systems.</description>
		<content:encoded><![CDATA[<p>&#8220;I can’t recall ever encountering a bug that would have been prevented by adopting this practice.&#8221;</p>
<p>You must have been using Java for very different things than myself.  </p>
<p>One of the things final fields help with is thread safety.  I can only assume you have never had a field change when you didn&#8217;t expect it to. or you were happy to just synchronize everything, or you only write single threaded code.<br />
I can only assume you have never used a field before it was initialized.<br />
I guess you are lucky that you have only worked with people who always understood all the assumption made about when a field is set and when it can be used.</p>
<p>Or perhaps you haven&#8217;t realized how many bugs you could have prevented with final fields.</p>
<p>“Maybe enough programmers will agree with you for it to catch on.”<br />
Unlikely. Most developers don&#8217;t like having to think about multi-threaded issues and most applications don&#8217;t require them to do so.</p>
<p>However if there were a compiler option I would certainly use it.</p>
<p>There is a measurable benefit in using final fields.  Its not big but applications do perform faster as the JVM can optimize access to such fields.</p>
<p>The biggest benefit is in allowing multi-threaded access without synchronization.  This does provide a significant benefit in highly multi-threaded systems.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-81490</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Mon, 28 Apr 2008 20:53:18 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-81490</guid>
		<description>&quot;At any rate, if you think this is a good idea, you should keep promoting it.&quot;

I&#039;ll do :-)

&quot;Maybe enough programmers will agree with you for it to catch on.&quot;

Not in Java I fear. Other communities have different ideas. But in the end I do not care. I really think reusing variables is a code smell and preventing reuse will lead to more and smaller methods, which only do one thing and do not try to achieve several things,  and in the end better code. My wish for a compiler switch is also more for me, not to dump it onto others. Perhaps IDEA will have a plugin sometimes to hide the final keyword.

Thanks for your thoughts on the topic.</description>
		<content:encoded><![CDATA[<p>&#8220;At any rate, if you think this is a good idea, you should keep promoting it.&#8221;</p>
<p>I&#8217;ll do :-)</p>
<p>&#8220;Maybe enough programmers will agree with you for it to catch on.&#8221;</p>
<p>Not in Java I fear. Other communities have different ideas. But in the end I do not care. I really think reusing variables is a code smell and preventing reuse will lead to more and smaller methods, which only do one thing and do not try to achieve several things,  and in the end better code. My wish for a compiler switch is also more for me, not to dump it onto others. Perhaps IDEA will have a plugin sometimes to hide the final keyword.</p>
<p>Thanks for your thoughts on the topic.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: DG</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-81485</link>
		<dc:creator>DG</dc:creator>
		<pubDate>Mon, 28 Apr 2008 20:47:58 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-81485</guid>
		<description>I&#039;ve had a few shadowed bugs in my career, but they would represent a minuscule percentage in the big picture. Certainly not enough to justify junking up my code with tons of superfluous variables.

And, to each his own, but I don&#039;t consider &quot;reusing&quot; variables to be a code smell. They&#039;re called &quot;variables&quot; for a reason.

At any rate, if you think this is a good idea, you should keep promoting it. Maybe enough programmers will agree with you for it to catch on. But I doubt that will be the case.</description>
		<content:encoded><![CDATA[<p>I&#8217;ve had a few shadowed bugs in my career, but they would represent a minuscule percentage in the big picture. Certainly not enough to justify junking up my code with tons of superfluous variables.</p>
<p>And, to each his own, but I don&#8217;t consider &#8220;reusing&#8221; variables to be a code smell. They&#8217;re called &#8220;variables&#8221; for a reason.</p>
<p>At any rate, if you think this is a good idea, you should keep promoting it. Maybe enough programmers will agree with you for it to catch on. But I doubt that will be the case.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-81470</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Mon, 28 Apr 2008 20:29:19 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-81470</guid>
		<description>@DG:

&quot;[...], but in other languages where variables are, well, variable&quot;

It&#039;s the default style of implementation in some languages, FP language for example favor final type assignments. See the comment from Saudade above.

&quot;[...] it’s absurd to think that any governing body of a language would make a change that would break probably 95% of the existing code base.&quot;

See my comment above: &quot;Migration should be supported with -final argument to the compiler, just like with other features of Java as you’ve said.&quot;

&quot;[...] I can’t recall ever encountering a bug that would have been prevented by adopting this practice.&quot;

Making all variables (well except the ones I declared var/mutable/transient, see above comments from me) final would result in differently - and I think better - organized and designed code, more with a functional flavor. And it would lead to less bugs. Perhaps you didn&#039;t have any aliased/shadowed bug in your career,  I had quite some in the last 25 years and they we&#039;re often hard to find.

&quot;(when one is all that’s really needed)&quot;

As written above, I consider reusing variables a code smell. See comment comment above.</description>
		<content:encoded><![CDATA[<p>@DG:</p>
<p>&#8220;[...], but in other languages where variables are, well, variable&#8221;</p>
<p>It&#8217;s the default style of implementation in some languages, FP language for example favor final type assignments. See the comment from Saudade above.</p>
<p>&#8220;[...] it’s absurd to think that any governing body of a language would make a change that would break probably 95% of the existing code base.&#8221;</p>
<p>See my comment above: &#8220;Migration should be supported with -final argument to the compiler, just like with other features of Java as you’ve said.&#8221;</p>
<p>&#8220;[...] I can’t recall ever encountering a bug that would have been prevented by adopting this practice.&#8221;</p>
<p>Making all variables (well except the ones I declared var/mutable/transient, see above comments from me) final would result in differently &#8211; and I think better &#8211; organized and designed code, more with a functional flavor. And it would lead to less bugs. Perhaps you didn&#8217;t have any aliased/shadowed bug in your career,  I had quite some in the last 25 years and they we&#8217;re often hard to find.</p>
<p>&#8220;(when one is all that’s really needed)&#8221;</p>
<p>As written above, I consider reusing variables a code smell. See comment comment above.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: DG</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-81432</link>
		<dc:creator>DG</dc:creator>
		<pubDate>Mon, 28 Apr 2008 19:17:34 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-81432</guid>
		<description>@stephan:

Even if your proposal, to make references (or identifiers, I guess I should say, so as to include primitives) final by default, were a good idea, which I&#039;m arguing it&#039;s not, it&#039;s absurd to think that any governing body of a language would make a change that would break probably 95% of the existing code base.

As for declaring all variables as final (at least where it&#039;s possible), I&#039;m sorry to be blunt, but I think it&#039;s a dumb idea, plain and simple. If there was a measurable benefit to be gained from doing it, it would have become common practice long ago, not just in Java, but in other languages where variables are, well, variable. 

Certain ideas are just obviously good, like for example global variables and goto statements should be avoided, and as a result they gain currency over time, and become conventional wisdom in the programming world. Seriously, no offense, but your idea is not one of them.

You say doing this can prevent bugs, but as I said, I can&#039;t recall ever encountering a bug that would have been prevented by adopting this practice. And even though it could conceivably prevent a bug on very rare occasions, I can more easily imagine scenarios where a proliferation of declared variables (when one is all that&#039;s really needed) could make bugs more likely, rather than less.</description>
		<content:encoded><![CDATA[<p>@stephan:</p>
<p>Even if your proposal, to make references (or identifiers, I guess I should say, so as to include primitives) final by default, were a good idea, which I&#8217;m arguing it&#8217;s not, it&#8217;s absurd to think that any governing body of a language would make a change that would break probably 95% of the existing code base.</p>
<p>As for declaring all variables as final (at least where it&#8217;s possible), I&#8217;m sorry to be blunt, but I think it&#8217;s a dumb idea, plain and simple. If there was a measurable benefit to be gained from doing it, it would have become common practice long ago, not just in Java, but in other languages where variables are, well, variable. </p>
<p>Certain ideas are just obviously good, like for example global variables and goto statements should be avoided, and as a result they gain currency over time, and become conventional wisdom in the programming world. Seriously, no offense, but your idea is not one of them.</p>
<p>You say doing this can prevent bugs, but as I said, I can&#8217;t recall ever encountering a bug that would have been prevented by adopting this practice. And even though it could conceivably prevent a bug on very rare occasions, I can more easily imagine scenarios where a proliferation of declared variables (when one is all that&#8217;s really needed) could make bugs more likely, rather than less.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-81407</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Mon, 28 Apr 2008 16:19:47 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-81407</guid>
		<description>I wanted to hack javac to do fun things for quite some time, especially since everyon hacks his own closure implementation :-)</description>
		<content:encoded><![CDATA[<p>I wanted to hack javac to do fun things for quite some time, especially since everyon hacks his own closure implementation :-)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Torgny Andersson</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-81381</link>
		<dc:creator>Torgny Andersson</dc:creator>
		<pubDate>Mon, 28 Apr 2008 14:22:56 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-81381</guid>
		<description>&quot;Not really, final is more a write-before-first-read.&quot;
I didn&#039;t know that... Hurray! I learn something new everyday! :)

&quot;The problem with the finally is that the variable is first read before written.&quot;
I have to argue that that&#039;s not a problem... it&#039;s a feature. :)
If you want that kind of behaviour I suggest that you write an AssignOnce class, such that you can do:

class Something {
  private final AssignOnce it = new AssignOnce();

  setIt(Object o) {
    it.set(o); // throws run-time exception if already set.
  }
}

Or, (and that&#039;s a much more fun solution) you make some delicate changes to javac. :)</description>
		<content:encoded><![CDATA[<p>&#8220;Not really, final is more a write-before-first-read.&#8221;<br />
I didn&#8217;t know that&#8230; Hurray! I learn something new everyday! :)</p>
<p>&#8220;The problem with the finally is that the variable is first read before written.&#8221;<br />
I have to argue that that&#8217;s not a problem&#8230; it&#8217;s a feature. :)<br />
If you want that kind of behaviour I suggest that you write an AssignOnce class, such that you can do:</p>
<p>class Something {<br />
  private final AssignOnce it = new AssignOnce();</p>
<p>  setIt(Object o) {<br />
    it.set(o); // throws run-time exception if already set.<br />
  }<br />
}</p>
<p>Or, (and that&#8217;s a much more fun solution) you make some delicate changes to javac. :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-81378</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Mon, 28 Apr 2008 14:10:20 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-81378</guid>
		<description>&quot;final: is assigned where the variable is declared; cannot be undefined/unassigned; cannot change value.&quot;

Not really, final is more a write-before-first-read.

&lt;code&gt;
final int a;
a = 3;
&lt;/code&gt; 

works as does

&lt;code&gt;
	private final int b;
	
	public A() {
		this.b = 3;
	}
&lt;/code&gt;

The problem with the finally is that the variable is first read before written.</description>
		<content:encoded><![CDATA[<p>&#8220;final: is assigned where the variable is declared; cannot be undefined/unassigned; cannot change value.&#8221;</p>
<p>Not really, final is more a write-before-first-read.</p>
<p><code><br />
final int a;<br />
a = 3;<br />
</code> </p>
<p>works as does</p>
<p><code><br />
	private final int b;</p>
<p>	public A() {<br />
		this.b = 3;<br />
	}<br />
</code></p>
<p>The problem with the finally is that the variable is first read before written.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: DG</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-81371</link>
		<dc:creator>DG</dc:creator>
		<pubDate>Mon, 28 Apr 2008 13:11:53 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-81371</guid>
		<description>If you make uninitialized local references null, then you&#039;re breaking the contract for final, because the reference will hold two legitimate values during its lifetime.

But that&#039;s all beside my main point, which was in the last sentence of my previous post. This proposal of yours is, IMO, a solution in search of a problem.</description>
		<content:encoded><![CDATA[<p>If you make uninitialized local references null, then you&#8217;re breaking the contract for final, because the reference will hold two legitimate values during its lifetime.</p>
<p>But that&#8217;s all beside my main point, which was in the last sentence of my previous post. This proposal of yours is, IMO, a solution in search of a problem.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Torgny Andersson</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-81370</link>
		<dc:creator>Torgny Andersson</dc:creator>
		<pubDate>Mon, 28 Apr 2008 13:10:30 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-81370</guid>
		<description>It sounds like what you really want is assign-once variables rather than final varibles. For me, there is a difference:

final: is assigned where the variable is declared; cannot be undefined/unassigned; cannot change value.

assign-once: is assign after being declared; can be undefined/unassigned; cannot change value once defined/assigned.

(Note that final variables is a subset of assign-once variables).

I would think that it it impossible (in the general case) to gurantee that assign-once variables are defined before they are read. This is, however, trivial to final variables. IOW, there will be run-time errors instead of compile-time errors if assign-once variable is used. 

However, it is alot easier for the developer to reason about assign-once variables than non-final varables. At least I can reason easire about assign-once variables.</description>
		<content:encoded><![CDATA[<p>It sounds like what you really want is assign-once variables rather than final varibles. For me, there is a difference:</p>
<p>final: is assigned where the variable is declared; cannot be undefined/unassigned; cannot change value.</p>
<p>assign-once: is assign after being declared; can be undefined/unassigned; cannot change value once defined/assigned.</p>
<p>(Note that final variables is a subset of assign-once variables).</p>
<p>I would think that it it impossible (in the general case) to gurantee that assign-once variables are defined before they are read. This is, however, trivial to final variables. IOW, there will be run-time errors instead of compile-time errors if assign-once variable is used. </p>
<p>However, it is alot easier for the developer to reason about assign-once variables than non-final varables. At least I can reason easire about assign-once variables.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-81356</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Mon, 28 Apr 2008 12:00:11 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-81356</guid>
		<description>It&#039;s not that the compiler shouldn&#039;t prevent the reading of uninitialized variables, but with a Null check guard this should work, if the contract is extended for uninitialized variables to be null (see my comment above). Otherwise your totally right of course.</description>
		<content:encoded><![CDATA[<p>It&#8217;s not that the compiler shouldn&#8217;t prevent the reading of uninitialized variables, but with a Null check guard this should work, if the contract is extended for uninitialized variables to be null (see my comment above). Otherwise your totally right of course.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: DG</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-2/#comment-81349</link>
		<dc:creator>DG</dc:creator>
		<pubDate>Mon, 28 Apr 2008 11:50:21 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-81349</guid>
		<description>I think that aspect of the Java compiler (preventing the reading of a possibly uninitialized variable) is a great feature. In my guess, it&#039;s prevented far more runtime bugs than making all references final would. After pushing out many, many thousands of lines of Java code over the years, I can&#039;t recall a single bug I ever found and fixed where I said to myself, &quot;gee, that wouldn&#039;t have happened if only I had declared that reference as final.&quot;</description>
		<content:encoded><![CDATA[<p>I think that aspect of the Java compiler (preventing the reading of a possibly uninitialized variable) is a great feature. In my guess, it&#8217;s prevented far more runtime bugs than making all references final would. After pushing out many, many thousands of lines of Java code over the years, I can&#8217;t recall a single bug I ever found and fixed where I said to myself, &#8220;gee, that wouldn&#8217;t have happened if only I had declared that reference as final.&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-81199</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Mon, 28 Apr 2008 04:23:25 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-81199</guid>
		<description>@DG: You&#039;re right, the current language syntax in Java does not allow to make such a var final. Or more clearly the compiler is to blame because it does not accept

if (finalVar != null) {
   ...
}

as a guard to an uninitialized variable.</description>
		<content:encoded><![CDATA[<p>@DG: You&#8217;re right, the current language syntax in Java does not allow to make such a var final. Or more clearly the compiler is to blame because it does not accept</p>
<p>if (finalVar != null) {<br />
   &#8230;<br />
}</p>
<p>as a guard to an uninitialized variable.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: DG</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-81077</link>
		<dc:creator>DG</dc:creator>
		<pubDate>Sun, 27 Apr 2008 23:15:30 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-81077</guid>
		<description>How would one close a Connection in a finally block that was opened in the corresponding try block? The only way I know is to declare the reference (variable) outside the try block and initialize it to null, then assign the opened Connection to it inside the try block, which is impossible if the reference is final.</description>
		<content:encoded><![CDATA[<p>How would one close a Connection in a finally block that was opened in the corresponding try block? The only way I know is to declare the reference (variable) outside the try block and initialize it to null, then assign the opened Connection to it inside the try block, which is impossible if the reference is final.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: fritz freiheit.com » Friday link dump</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-79913</link>
		<dc:creator>fritz freiheit.com » Friday link dump</dc:creator>
		<pubDate>Fri, 25 Apr 2008 19:11:50 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-79913</guid>
		<description>[...] All variables in Java must be final at Stephans Blog (Java) [...]</description>
		<content:encoded><![CDATA[<p>[...] All variables in Java must be final at Stephans Blog (Java) [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Torgny Andersson</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-79113</link>
		<dc:creator>Torgny Andersson</dc:creator>
		<pubDate>Thu, 24 Apr 2008 10:45:42 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-79113</guid>
		<description>&quot;And your dynamic scope is crazy stuff. I really like people pushing Javas boundaries, it feels like when we push 6502 boundaries 25 years ago.&quot;
It&#039;s powerful but dangerous stuff, I think... :)

The closest I&#039;ve got the a 6502 is when I programmed in Basic on the C64. Ah, such fond memories. :)</description>
		<content:encoded><![CDATA[<p>&#8220;And your dynamic scope is crazy stuff. I really like people pushing Javas boundaries, it feels like when we push 6502 boundaries 25 years ago.&#8221;<br />
It&#8217;s powerful but dangerous stuff, I think&#8230; :)</p>
<p>The closest I&#8217;ve got the a 6502 is when I programmed in Basic on the C64. Ah, such fond memories. :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78982</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Thu, 24 Apr 2008 04:37:50 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78982</guid>
		<description>@Behrang: A closure based each doesn&#039;t eliminate it either. Someone has to loop over the collection. But it&#039;s not me, the caller, which has to do it, it&#039;s not my code. 

If Java &lt;code&gt;Collections&lt;/code&gt; would provide an &lt;code&gt;apply()&lt;/code&gt; method, the implicit loop would be there, in infrastructure code. i don&#039;t care if they use final or not. 

Rubys &lt;code&gt;each&lt;/code&gt; needs to have an internal loop too, but it&#039;s part of the language library not part of my code.

http://martinfowler.com/bliki/Closure.html

That was the point I wished to make.

&lt;code&gt;
Department.java:
public void persons(Applyable applyable) {
    this.persons.apply(applyable);
}
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>@Behrang: A closure based each doesn&#8217;t eliminate it either. Someone has to loop over the collection. But it&#8217;s not me, the caller, which has to do it, it&#8217;s not my code. </p>
<p>If Java <code>Collections</code> would provide an <code>apply()</code> method, the implicit loop would be there, in infrastructure code. i don&#8217;t care if they use final or not. </p>
<p>Rubys <code>each</code> needs to have an internal loop too, but it&#8217;s part of the language library not part of my code.</p>
<p><a href="http://martinfowler.com/bliki/Closure.html" rel="nofollow">http://martinfowler.com/bliki/Closure.html</a></p>
<p>That was the point I wished to make.</p>
<p><code><br />
Department.java:<br />
public void persons(Applyable applyable) {<br />
    this.persons.apply(applyable);<br />
}<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Behrang</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78763</link>
		<dc:creator>Behrang</dc:creator>
		<pubDate>Wed, 23 Apr 2008 21:31:45 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78763</guid>
		<description>&quot;Can’t do? Well the example does it.&quot;

Stephan, could you please show me the completed forEach method? Or the completed each method of the Applyable class? The Applyable class moves the loop from the caller code into the callee but it does not eliminate it.</description>
		<content:encoded><![CDATA[<p>&#8220;Can’t do? Well the example does it.&#8221;</p>
<p>Stephan, could you please show me the completed forEach method? Or the completed each method of the Applyable class? The Applyable class moves the loop from the caller code into the callee but it does not eliminate it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: peter lawrey</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78704</link>
		<dc:creator>peter lawrey</dc:creator>
		<pubDate>Wed, 23 Apr 2008 20:10:55 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78704</guid>
		<description>There are other things you can do with closures which you cannot do easily with anonymous classes.

1) Change local variables.

boolean found = false;
forEach(Person person) {
   if (person.name.equals(name))
      found = true;
      break;
   }
}
// found may or may not have changed.

2) break from a &quot;loop&quot;
Rather like a return from the loop.

3) continue in a &quot;loop&quot;
skip the rest of the anonymous method.

4) return in a &quot;loop&quot; from the method. This doesn&#039;t return from the anonymous class 
public Person find(String name) {
forEach(Person person) {
   if (person.name.equals(name))
      return person;
   }
}
// do some more searching
// ....
}

5) Throw checked exceptions.  The forEach throws whatever CheckedException&#039;s the closure throws.

public Person find(String name) throws InvalidPersonException {
forEach(Person person) {
   if (person.name == null)
      throw new InvalidPersonException(&quot;null name&quot;)l
}
}

6) break or continue with a label.
The following has two nested closures....

LOOP: forEach(Person person) {
    forEach(Role role) {
       if (!person.roles.isAMemberOf(role))
          continue LOOP;
       if (!person.isActive())
          continue;
    }
}</description>
		<content:encoded><![CDATA[<p>There are other things you can do with closures which you cannot do easily with anonymous classes.</p>
<p>1) Change local variables.</p>
<p>boolean found = false;<br />
forEach(Person person) {<br />
   if (person.name.equals(name))<br />
      found = true;<br />
      break;<br />
   }<br />
}<br />
// found may or may not have changed.</p>
<p>2) break from a &#8220;loop&#8221;<br />
Rather like a return from the loop.</p>
<p>3) continue in a &#8220;loop&#8221;<br />
skip the rest of the anonymous method.</p>
<p>4) return in a &#8220;loop&#8221; from the method. This doesn&#8217;t return from the anonymous class<br />
public Person find(String name) {<br />
forEach(Person person) {<br />
   if (person.name.equals(name))<br />
      return person;<br />
   }<br />
}<br />
// do some more searching<br />
// &#8230;.<br />
}</p>
<p>5) Throw checked exceptions.  The forEach throws whatever CheckedException&#8217;s the closure throws.</p>
<p>public Person find(String name) throws InvalidPersonException {<br />
forEach(Person person) {<br />
   if (person.name == null)<br />
      throw new InvalidPersonException(&#8220;null name&#8221;)l<br />
}<br />
}</p>
<p>6) break or continue with a label.<br />
The following has two nested closures&#8230;.</p>
<p>LOOP: forEach(Person person) {<br />
    forEach(Role role) {<br />
       if (!person.roles.isAMemberOf(role))<br />
          continue LOOP;<br />
       if (!person.isActive())<br />
          continue;<br />
    }<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78680</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Wed, 23 Apr 2008 19:18:44 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78680</guid>
		<description>And your dynamic scope is crazy stuff. I really like people pushing Javas boundaries, it feels like when we push 6502 boundaries 25 years ago.</description>
		<content:encoded><![CDATA[<p>And your dynamic scope is crazy stuff. I really like people pushing Javas boundaries, it feels like when we push 6502 boundaries 25 years ago.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78678</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Wed, 23 Apr 2008 19:15:54 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78678</guid>
		<description>:-)</description>
		<content:encoded><![CDATA[<p>:-)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Torgny Andersson</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78674</link>
		<dc:creator>Torgny Andersson</dc:creator>
		<pubDate>Wed, 23 Apr 2008 19:06:57 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78674</guid>
		<description>Crap, you got me. :)</description>
		<content:encoded><![CDATA[<p>Crap, you got me. :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78673</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Wed, 23 Apr 2008 18:53:50 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78673</guid>
		<description>Well, when it&#039;s about noise, then syntactic sugar to annonymous classe should be enough :-) But we&#039;ll see with Java 7.</description>
		<content:encoded><![CDATA[<p>Well, when it&#8217;s about noise, then syntactic sugar to annonymous classe should be enough :-) But we&#8217;ll see with Java 7.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Torgny Andersson</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78671</link>
		<dc:creator>Torgny Andersson</dc:creator>
		<pubDate>Wed, 23 Apr 2008 18:27:55 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78671</guid>
		<description>Sure, there are work-arounds, but the whole point with closures (at least for me) is that noisy code becomes easier to read and understand.

Let&#039;s wait and see what Java 7 will be like...</description>
		<content:encoded><![CDATA[<p>Sure, there are work-arounds, but the whole point with closures (at least for me) is that noisy code becomes easier to read and understand.</p>
<p>Let&#8217;s wait and see what Java 7 will be like&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78661</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Wed, 23 Apr 2008 17:34:12 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78661</guid>
		<description>And probably very buggy.</description>
		<content:encoded><![CDATA[<p>And probably very buggy.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78658</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Wed, 23 Apr 2008 17:27:37 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78658</guid>
		<description>Thanks.

You could fix this with a 

if (predicate) {
  return true;
}
return false;

Not as nice, but possible with another &lt;code&gt;Loop&lt;/code&gt; implementation.</description>
		<content:encoded><![CDATA[<p>Thanks.</p>
<p>You could fix this with a </p>
<p>if (predicate) {<br />
  return true;<br />
}<br />
return false;</p>
<p>Not as nice, but possible with another <code>Loop</code> implementation.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Torgny Andersson</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78635</link>
		<dc:creator>Torgny Andersson</dc:creator>
		<pubDate>Wed, 23 Apr 2008 14:33:58 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78635</guid>
		<description>When you call &quot;return&quot; in a closure, you return from the method that is executing the closure.

That is, in the first of my two examples the &#039;return&#039; statement means &quot;return from the surrounding method, i.e., the method that contains the forEach-loop&quot;.
On the other hand, in the secound example the &#039;return&#039; statement means &quot;return from the &#039;iteration&#039; method&quot;.

In other words, the &#039;return&#039; statement will stop the iteration in the first example, but will not stop the iteration in the second example.

I hope this explains what I mean. Sorry for being unclear earlier.</description>
		<content:encoded><![CDATA[<p>When you call &#8220;return&#8221; in a closure, you return from the method that is executing the closure.</p>
<p>That is, in the first of my two examples the &#8216;return&#8217; statement means &#8220;return from the surrounding method, i.e., the method that contains the forEach-loop&#8221;.<br />
On the other hand, in the secound example the &#8216;return&#8217; statement means &#8220;return from the &#8216;iteration&#8217; method&#8221;.</p>
<p>In other words, the &#8216;return&#8217; statement will stop the iteration in the first example, but will not stop the iteration in the second example.</p>
<p>I hope this explains what I mean. Sorry for being unclear earlier.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78628</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Wed, 23 Apr 2008 14:06:15 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78628</guid>
		<description>@Torgny: Could you explain why it does not have the same semantics?</description>
		<content:encoded><![CDATA[<p>@Torgny: Could you explain why it does not have the same semantics?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Torgny Andersson</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78606</link>
		<dc:creator>Torgny Andersson</dc:creator>
		<pubDate>Wed, 23 Apr 2008 12:20:56 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78606</guid>
		<description>I have to disagree: Java does not have closures -- Java has anonymous classes. On thing that can be done with closures that cannot be done with anonymous classes is the following:
forEach(collection) {
  if (predicate) {
    return;
  }
}

A naive mapping to anonymous classes whould result in something like:
forEach(collection, new Loop() {
  void iteration()
    if (predicate) {
      return;
    }
  }
}
which _does_not_ have the same semantics as the closure-example above.

This is one reason Java needs closures in my opinion.</description>
		<content:encoded><![CDATA[<p>I have to disagree: Java does not have closures &#8212; Java has anonymous classes. On thing that can be done with closures that cannot be done with anonymous classes is the following:<br />
forEach(collection) {<br />
  if (predicate) {<br />
    return;<br />
  }<br />
}</p>
<p>A naive mapping to anonymous classes whould result in something like:<br />
forEach(collection, new Loop() {<br />
  void iteration()<br />
    if (predicate) {<br />
      return;<br />
    }<br />
  }<br />
}<br />
which _does_not_ have the same semantics as the closure-example above.</p>
<p>This is one reason Java needs closures in my opinion.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78590</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Wed, 23 Apr 2008 12:06:20 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78590</guid>
		<description>@Behrang: Can&#039;t do? Well the example does it. Anonymous classes in Java are closures. What people most often mean when they say Java has no closures:

1.) There is no short syntax for closures (See Java 7 proposals)
2.) The libraries don&#039;t &quot;think&quot; in closures

But all Java collections could support a map(Mapper) method and it would be - more noisy - but the same as map in Ruby/Python/...</description>
		<content:encoded><![CDATA[<p>@Behrang: Can&#8217;t do? Well the example does it. Anonymous classes in Java are closures. What people most often mean when they say Java has no closures:</p>
<p>1.) There is no short syntax for closures (See Java 7 proposals)<br />
2.) The libraries don&#8217;t &#8220;think&#8221; in closures</p>
<p>But all Java collections could support a map(Mapper) method and it would be &#8211; more noisy &#8211; but the same as map in Ruby/Python/&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Behrang</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78561</link>
		<dc:creator>Behrang</dc:creator>
		<pubDate>Wed, 23 Apr 2008 10:33:25 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78561</guid>
		<description>@Stephan

Without closures you can&#039;t do that in Java unless you use some kind of iteration.</description>
		<content:encoded><![CDATA[<p>@Stephan</p>
<p>Without closures you can&#8217;t do that in Java unless you use some kind of iteration.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: peter lawrey</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78529</link>
		<dc:creator>peter lawrey</dc:creator>
		<pubDate>Wed, 23 Apr 2008 07:20:30 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78529</guid>
		<description>There should be an extra step for making a variable non-final.  The point is that a high percentage of non-final variable could be final and more importantly could be made final with just a little though, a process which is currently missing.

Closures make your loop code much shorter.

forEach(Person p: department) {
   // what ever.
}

Note: the default modifiers for interface are final for fields and non-final for classes. In the same way the default modifier for fields can be final, but the default for loop variables could be non-final.  If you want to be explicit everywhere you still could.
I would suggest &#039;var&#039; be the keyword as it is used for javascript to do the same thing.</description>
		<content:encoded><![CDATA[<p>There should be an extra step for making a variable non-final.  The point is that a high percentage of non-final variable could be final and more importantly could be made final with just a little though, a process which is currently missing.</p>
<p>Closures make your loop code much shorter.</p>
<p>forEach(Person p: department) {<br />
   // what ever.<br />
}</p>
<p>Note: the default modifiers for interface are final for fields and non-final for classes. In the same way the default modifier for fields can be final, but the default for loop variables could be non-final.  If you want to be explicit everywhere you still could.<br />
I would suggest &#8216;var&#8217; be the keyword as it is used for javascript to do the same thing.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78481</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Wed, 23 Apr 2008 05:13:51 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78481</guid>
		<description>See http://www.pragprog.com/articles/tell-dont-ask for the usage of &lt;code&gt;Applyable&lt;/code&gt;.</description>
		<content:encoded><![CDATA[<p>See <a href="http://www.pragprog.com/articles/tell-dont-ask" rel="nofollow">http://www.pragprog.com/articles/tell-dont-ask</a> for the usage of <code>Applyable</code>.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78478</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Wed, 23 Apr 2008 05:11:31 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78478</guid>
		<description>@Behrang: Some people think, and I might be one of them, that you should not iterate at all.

Instead of 

&lt;pre name=&quot;code&quot; class=&quot;java:nocontrols:nogutter&quot;&gt;
for (Person person: department.getPersons()) {
...
}
&lt;/pre&gt;

One could write
&lt;pre name=&quot;code&quot; class=&quot;java:nocontrols:nogutter&quot;&gt;
department.eachPerson( new Loop&lt;Person&gt;() {
    public void doWith(Person person) {
       ...
    }
}
&lt;/pre&gt;

which is longer and more noisy but a.) doesn&#039;t make the caller dependent of the Person implementation (Iterator or Collection) b.) the Loop code could be used in different places.

Java isn&#039;t the best language to do this, but it&#039;s possible.

Beside that, you could use a keyword to make the variable not final.</description>
		<content:encoded><![CDATA[<p>@Behrang: Some people think, and I might be one of them, that you should not iterate at all.</p>
<p>Instead of </p>
<pre name="code" class="java:nocontrols:nogutter">
for (Person person: department.getPersons()) {
...
}
</pre>
<p>One could write</p>
<pre name="code" class="java:nocontrols:nogutter">
department.eachPerson( new Loop&lt;Person&gt;() {
    public void doWith(Person person) {
       ...
    }
}
</pre>
<p>which is longer and more noisy but a.) doesn&#8217;t make the caller dependent of the Person implementation (Iterator or Collection) b.) the Loop code could be used in different places.</p>
<p>Java isn&#8217;t the best language to do this, but it&#8217;s possible.</p>
<p>Beside that, you could use a keyword to make the variable not final.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Behrang</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78469</link>
		<dc:creator>Behrang</dc:creator>
		<pubDate>Wed, 23 Apr 2008 05:01:12 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78469</guid>
		<description>&quot;I usually iterate over a collection not a loop.&quot;

LOL. I wanted to say the same thing. Also sometimes you just need to create loops just for the sake of implementing various kinds of algorithms. And even almost all iterator implementation need non-final variables. There still are a billion of places in which you need non-final variables.</description>
		<content:encoded><![CDATA[<p>&#8220;I usually iterate over a collection not a loop.&#8221;</p>
<p>LOL. I wanted to say the same thing. Also sometimes you just need to create loops just for the sake of implementing various kinds of algorithms. And even almost all iterator implementation need non-final variables. There still are a billion of places in which you need non-final variables.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78463</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Wed, 23 Apr 2008 04:50:29 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78463</guid>
		<description>&quot;So how do you iterate over a loop?&quot; 

I usually iterate over a collection not a loop.</description>
		<content:encoded><![CDATA[<p>&#8220;So how do you iterate over a loop?&#8221; </p>
<p>I usually iterate over a collection not a loop.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shams</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78431</link>
		<dc:creator>Shams</dc:creator>
		<pubDate>Wed, 23 Apr 2008 02:59:37 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78431</guid>
		<description>@stephan regarding null checks

public void doSomething(input) {  
  
  if (input == null) 
    input = myDefaultValue;  

  final result = calc(input);  
} 

vs

public void doSomething(final input) {  
  
  final result;  
  if (input == null) 
    result = calc(myDefaultValue);
  else 
    result = calc(input);    

}</description>
		<content:encoded><![CDATA[<p>@stephan regarding null checks</p>
<p>public void doSomething(input) {  </p>
<p>  if (input == null)<br />
    input = myDefaultValue;  </p>
<p>  final result = calc(input);<br />
} </p>
<p>vs</p>
<p>public void doSomething(final input) {  </p>
<p>  final result;<br />
  if (input == null)<br />
    result = calc(myDefaultValue);<br />
  else<br />
    result = calc(input);    </p>
<p>}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Behrang</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78312</link>
		<dc:creator>Behrang</dc:creator>
		<pubDate>Tue, 22 Apr 2008 23:21:15 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78312</guid>
		<description>Oh, I see. You recommend having a new keyword called transient. Nah. I don&#039;t like it. Then I should declare half of my variables or maybe even more transient.</description>
		<content:encoded><![CDATA[<p>Oh, I see. You recommend having a new keyword called transient. Nah. I don&#8217;t like it. Then I should declare half of my variables or maybe even more transient.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Behrang</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78309</link>
		<dc:creator>Behrang</dc:creator>
		<pubDate>Tue, 22 Apr 2008 23:18:02 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78309</guid>
		<description>So how do you iterate over a loop? Or keep the current stack position? Or...? Or how do you change the date of birth of a Person object?</description>
		<content:encoded><![CDATA[<p>So how do you iterate over a loop? Or keep the current stack position? Or&#8230;? Or how do you change the date of birth of a Person object?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78207</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Tue, 22 Apr 2008 21:40:12 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78207</guid>
		<description>@choucri: I&#039;ve been following Scala for a long time and did some minor stuff with Scala. From my perspective it&#039;s too inconsistent for the average Java - enterprise - developer. But I guess the bet is still out for Scala, but I lean on the pessimistic side.

http://stephan.reposita.org/archives/2008/01/21/no-future-for-functional-programming-in-2008-scala-f-and-nu/</description>
		<content:encoded><![CDATA[<p>@choucri: I&#8217;ve been following Scala for a long time and did some minor stuff with Scala. From my perspective it&#8217;s too inconsistent for the average Java &#8211; enterprise &#8211; developer. But I guess the bet is still out for Scala, but I lean on the pessimistic side.</p>
<p><a href="http://stephan.reposita.org/archives/2008/01/21/no-future-for-functional-programming-in-2008-scala-f-and-nu/" rel="nofollow">http://stephan.reposita.org/archives/2008/01/21/no-future-for-functional-programming-in-2008-scala-f-and-nu/</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: choucri</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78203</link>
		<dc:creator>choucri</dc:creator>
		<pubDate>Tue, 22 Apr 2008 21:36:47 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78203</guid>
		<description>You sound like someone who would love to discover Scala! Check this link:
http://www.codecommit.com/blog/scala/roundup-scala-for-java-refugees</description>
		<content:encoded><![CDATA[<p>You sound like someone who would love to discover Scala! Check this link:<br />
<a href="http://www.codecommit.com/blog/scala/roundup-scala-for-java-refugees" rel="nofollow">http://www.codecommit.com/blog/scala/roundup-scala-for-java-refugees</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78180</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Tue, 22 Apr 2008 20:49:23 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78180</guid>
		<description>@Torgny: :-)</description>
		<content:encoded><![CDATA[<p>@Torgny: :-)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Torgny Andersson</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78179</link>
		<dc:creator>Torgny Andersson</dc:creator>
		<pubDate>Tue, 22 Apr 2008 20:48:14 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78179</guid>
		<description>&quot;Noone should use inheritance because it was a stupid idea from the beginning, use composition!&quot;
Again, I totaly agree. I even wrote a post about it a few days ago. :)</description>
		<content:encoded><![CDATA[<p>&#8220;Noone should use inheritance because it was a stupid idea from the beginning, use composition!&#8221;<br />
Again, I totaly agree. I even wrote a post about it a few days ago. :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78178</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Tue, 22 Apr 2008 20:41:23 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78178</guid>
		<description>@peter: It&#039;s never too late. And Java has shown that it can adapt, very slowly over several years, but it happens. See the very late changes to volatile :-)</description>
		<content:encoded><![CDATA[<p>@peter: It&#8217;s never too late. And Java has shown that it can adapt, very slowly over several years, but it happens. See the very late changes to volatile :-)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: peter lawrey</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78176</link>
		<dc:creator>peter lawrey</dc:creator>
		<pubDate>Tue, 22 Apr 2008 20:27:38 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78176</guid>
		<description>In hindsight, my preference would be that fields be final by default and there should be a keyword to highlight variable fields. like var?
I also think const should declare a reference as immutable, but I think its too late to add this now...</description>
		<content:encoded><![CDATA[<p>In hindsight, my preference would be that fields be final by default and there should be a keyword to highlight variable fields. like var?<br />
I also think const should declare a reference as immutable, but I think its too late to add this now&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78163</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Tue, 22 Apr 2008 19:41:20 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78163</guid>
		<description>@Chiral: Migration should be supported with -final argument to the compiler, just like with other features of Java as you&#039;ve said.</description>
		<content:encoded><![CDATA[<p>@Chiral: Migration should be supported with -final argument to the compiler, just like with other features of Java as you&#8217;ve said.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ChiralSoftware</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78160</link>
		<dc:creator>ChiralSoftware</dc:creator>
		<pubDate>Tue, 22 Apr 2008 19:03:27 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78160</guid>
		<description>Re: the &quot;transient&quot; keyword: I agree, it should be dropped as a keyword, and replaced with a @Transient annotation.

Making all &quot;variables&quot; final by default would be a nice change but I&#039;m not sure if they would consider doing something like that, given how many millions of lines of existing code it would probably break.  I guess they could have a compile-time flag that says which language version it is using.  This is a compiler change, not a bytecode issue.</description>
		<content:encoded><![CDATA[<p>Re: the &#8220;transient&#8221; keyword: I agree, it should be dropped as a keyword, and replaced with a @Transient annotation.</p>
<p>Making all &#8220;variables&#8221; final by default would be a nice change but I&#8217;m not sure if they would consider doing something like that, given how many millions of lines of existing code it would probably break.  I guess they could have a compile-time flag that says which language version it is using.  This is a compiler change, not a bytecode issue.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78154</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Tue, 22 Apr 2008 18:38:16 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78154</guid>
		<description>@raveman: The problem with inheritance is that most extending classes do not take the  pre/post-conditions of the class into account they are extending, or even don&#039;t know about them (Java vs. Eiffel). Final might help to prevent bugs from inheritance, when people don&#039;t know what they do. And by default: &lt;b&gt;Noone should use inheritance because it was a stupid idea from the beginning, use composition!&lt;/b&gt;. Only inherit in frameworks.</description>
		<content:encoded><![CDATA[<p>@raveman: The problem with inheritance is that most extending classes do not take the  pre/post-conditions of the class into account they are extending, or even don&#8217;t know about them (Java vs. Eiffel). Final might help to prevent bugs from inheritance, when people don&#8217;t know what they do. And by default: <b>Noone should use inheritance because it was a stupid idea from the beginning, use composition!</b>. Only inherit in frameworks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78153</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Tue, 22 Apr 2008 18:34:32 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78153</guid>
		<description>@kj: I&#039;m not sure if the &lt;code&gt;const&lt;/code&gt; keyword corresponds to all uses of final in Java. It&#039;s more or less about immutable attributes, and yes we should be able (annotation?) to tell about immutable objects.

(and returns from &lt;code&gt;Collections.unmodifiableCollection&lt;/code&gt; which cannot be differntiated from normal Lists in the API)</description>
		<content:encoded><![CDATA[<p>@kj: I&#8217;m not sure if the <code>const</code> keyword corresponds to all uses of final in Java. It&#8217;s more or less about immutable attributes, and yes we should be able (annotation?) to tell about immutable objects.</p>
<p>(and returns from <code>Collections.unmodifiableCollection</code> which cannot be differntiated from normal Lists in the API)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: raveman</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78152</link>
		<dc:creator>raveman</dc:creator>
		<pubDate>Tue, 22 Apr 2008 18:33:14 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78152</guid>
		<description>i disagree, for example you said that final lead to less bugs, for example if I have code

public final void doSomething(final String string, final String string2) {
     final String trimedString = string.trim();
     final String substringedString = string2.substring(1);
     return trimedString   substringedString;
}

i would argue that it can be more buggy if you have 2 variables more.

public void doSomething(String string, String string2) {
     string = string.trim();
     string2 = string2.substring(1);
     return string   string2;
}


I used to do final variables, but when i was changing the code removing final felt weird.

&quot;I only make classes non-final if I am designing for inheritance&quot;
its weird idea too. When you do that you make final even more stupid since it only means that now you dont extend that class, maybe in future you will, but then you can remove the final and call it refactoring.

and what about transient ?? i say use it alot too, if you not serializing class it should have all its field transient, just to tell people that youre not into serializing. Some wierd guy might try to serialize your class, but its YOUR class, stop him now! (with AOP you can make class implements Serializable at runtime, so you should use transient)</description>
		<content:encoded><![CDATA[<p>i disagree, for example you said that final lead to less bugs, for example if I have code</p>
<p>public final void doSomething(final String string, final String string2) {<br />
     final String trimedString = string.trim();<br />
     final String substringedString = string2.substring(1);<br />
     return trimedString   substringedString;<br />
}</p>
<p>i would argue that it can be more buggy if you have 2 variables more.</p>
<p>public void doSomething(String string, String string2) {<br />
     string = string.trim();<br />
     string2 = string2.substring(1);<br />
     return string   string2;<br />
}</p>
<p>I used to do final variables, but when i was changing the code removing final felt weird.</p>
<p>&#8220;I only make classes non-final if I am designing for inheritance&#8221;<br />
its weird idea too. When you do that you make final even more stupid since it only means that now you dont extend that class, maybe in future you will, but then you can remove the final and call it refactoring.</p>
<p>and what about transient ?? i say use it alot too, if you not serializing class it should have all its field transient, just to tell people that youre not into serializing. Some wierd guy might try to serialize your class, but its YOUR class, stop him now! (with AOP you can make class implements Serializable at runtime, so you should use transient)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: kj</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78151</link>
		<dc:creator>kj</dc:creator>
		<pubDate>Tue, 22 Apr 2008 18:30:37 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78151</guid>
		<description>What we really need is the const keyword, compare with the c   keyword...

public const String getId() {
  return copy-to-const(this.id);
}

....

public const /* do not change object state */ ...() {
  ...
}
public void ...(const String t) {
}</description>
		<content:encoded><![CDATA[<p>What we really need is the const keyword, compare with the c   keyword&#8230;</p>
<p>public const String getId() {<br />
  return copy-to-const(this.id);<br />
}</p>
<p>&#8230;.</p>
<p>public const /* do not change object state */ &#8230;() {<br />
  &#8230;<br />
}<br />
public void &#8230;(const String t) {<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78150</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Tue, 22 Apr 2008 18:29:09 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78150</guid>
		<description>@Saudade: Functional Programming is a state of mind not a programming language. So people can code FP in Java. Or there are atoms of principles from which programming languages derive their style: immutable declarations and statelessness are two of them, both can be realized with an FP language or with an OO language. FP is build on those, not equals those. Some people confuse the two facts.

@ChiralSoftware: Excatly, as I said &lt;code&gt;transient&lt;/code&gt; should be a keyword (and the current use dropped) for those. 

@Michael: Yes I agree, too much noise. An intelligent IDE could help. 

@Torgny: &quot;I think that the ratio between final and non-final variables is an estimation of how hard it is to understand a piece of code, because it gives an indication of how much state the code has.&quot; 

Excellent point.</description>
		<content:encoded><![CDATA[<p>@Saudade: Functional Programming is a state of mind not a programming language. So people can code FP in Java. Or there are atoms of principles from which programming languages derive their style: immutable declarations and statelessness are two of them, both can be realized with an FP language or with an OO language. FP is build on those, not equals those. Some people confuse the two facts.</p>
<p>@ChiralSoftware: Excatly, as I said <code>transient</code> should be a keyword (and the current use dropped) for those. </p>
<p>@Michael: Yes I agree, too much noise. An intelligent IDE could help. </p>
<p>@Torgny: &#8220;I think that the ratio between final and non-final variables is an estimation of how hard it is to understand a piece of code, because it gives an indication of how much state the code has.&#8221; </p>
<p>Excellent point.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Saudade</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78149</link>
		<dc:creator>Saudade</dc:creator>
		<pubDate>Tue, 22 Apr 2008 18:05:41 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78149</guid>
		<description>Functional programming!</description>
		<content:encoded><![CDATA[<p>Functional programming!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ChiralSoftware</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78135</link>
		<dc:creator>ChiralSoftware</dc:creator>
		<pubDate>Tue, 22 Apr 2008 17:39:04 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78135</guid>
		<description>final is the most frequently used keyword in my code.  It should be by default, with some other keyword, like &quot;mutable&quot;, required to make a variable non-final.  I&#039;m glad I&#039;m not the only one who has &quot;final&quot; all over the place in my code.</description>
		<content:encoded><![CDATA[<p>final is the most frequently used keyword in my code.  It should be by default, with some other keyword, like &#8220;mutable&#8221;, required to make a variable non-final.  I&#8217;m glad I&#8217;m not the only one who has &#8220;final&#8221; all over the place in my code.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michael Easter</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78133</link>
		<dc:creator>Michael Easter</dc:creator>
		<pubDate>Tue, 22 Apr 2008 17:35:39 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78133</guid>
		<description>I disagree for using final for method parameters and as a blanket statement for local variables.

For members/immutability, classes, etc, no problem.

But for the method params, it just too much noise.  Same for locals, usually.  I will use them occasionally but not by dogma.

It is true that I have a vulnerability for bugs, but it is very small and a good set of unit tests should expose it.  The unit tests are key, because they not only handle this issue but much, much more.

Mike

ps. For language design, it would be interesting to have everything to default to final unless otherwise stated.  Alas, with Java, I don&#039;t see it happening.</description>
		<content:encoded><![CDATA[<p>I disagree for using final for method parameters and as a blanket statement for local variables.</p>
<p>For members/immutability, classes, etc, no problem.</p>
<p>But for the method params, it just too much noise.  Same for locals, usually.  I will use them occasionally but not by dogma.</p>
<p>It is true that I have a vulnerability for bugs, but it is very small and a good set of unit tests should expose it.  The unit tests are key, because they not only handle this issue but much, much more.</p>
<p>Mike</p>
<p>ps. For language design, it would be interesting to have everything to default to final unless otherwise stated.  Alas, with Java, I don&#8217;t see it happening.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Torgny Andersson</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78127</link>
		<dc:creator>Torgny Andersson</dc:creator>
		<pubDate>Tue, 22 Apr 2008 17:11:38 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78127</guid>
		<description>I couldn&#039;t agree more: all variables should be final. Most variables are not assigned more than once anyway in well designed software, at least to my experience.

I think that the ratio between final and non-final variables is an estimation of how hard it is to understand a piece of code, because it gives an indication of how much state the code has.

In Eclipse at least, it is actually possible to configure the compile such that it generates an error if a method parameter is assigend. One step in the right direction, I think.</description>
		<content:encoded><![CDATA[<p>I couldn&#8217;t agree more: all variables should be final. Most variables are not assigned more than once anyway in well designed software, at least to my experience.</p>
<p>I think that the ratio between final and non-final variables is an estimation of how hard it is to understand a piece of code, because it gives an indication of how much state the code has.</p>
<p>In Eclipse at least, it is actually possible to configure the compile such that it generates an error if a method parameter is assigend. One step in the right direction, I think.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78126</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Tue, 22 Apr 2008 16:48:03 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78126</guid>
		<description>@pwilder: Thanks, I know about transient which is for non serializable attributes and is used - as far as I know - also used with JPA. What I meant was a transient keyword for non final declarations. It would fit I think.

@Ian: There was a myth some years ago when people put final everywhere for more performance, nowadays I think the Hotspot VM JIT is far outperforms final optimization by hand.

@Shams: What do you mean with &quot;null checks&quot;?

@matthew: &quot;[...] Object.freeze() method in Ruby, because it seems to be frowned upon. &quot; as a mindset this might lead to problems down the road with 500 classes and 100 developers. I&#039;ll look into &lt;code&gt;freeze&lt;/code&gt; thanks for the tip.</description>
		<content:encoded><![CDATA[<p>@pwilder: Thanks, I know about transient which is for non serializable attributes and is used &#8211; as far as I know &#8211; also used with JPA. What I meant was a transient keyword for non final declarations. It would fit I think.</p>
<p>@Ian: There was a myth some years ago when people put final everywhere for more performance, nowadays I think the Hotspot VM JIT is far outperforms final optimization by hand.</p>
<p>@Shams: What do you mean with &#8220;null checks&#8221;?</p>
<p>@matthew: &#8220;[...] Object.freeze() method in Ruby, because it seems to be frowned upon. &#8221; as a mindset this might lead to problems down the road with 500 classes and 100 developers. I&#8217;ll look into <code>freeze</code> thanks for the tip.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: pwilder</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78123</link>
		<dc:creator>pwilder</dc:creator>
		<pubDate>Tue, 22 Apr 2008 16:18:31 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78123</guid>
		<description>I couldn&#039;t tell from your post but just in case you didn&#039;t know there is already a rarely used transient Java key word. 

http://en.wikibooks.org/wiki/Java_Programming/Keywords/transient

Sorry I couldn&#039;t find a Sun reference.</description>
		<content:encoded><![CDATA[<p>I couldn&#8217;t tell from your post but just in case you didn&#8217;t know there is already a rarely used transient Java key word. </p>
<p><a href="http://en.wikibooks.org/wiki/Java_Programming/Keywords/transient" rel="nofollow">http://en.wikibooks.org/wiki/Java_Programming/Keywords/transient</a></p>
<p>Sorry I couldn&#8217;t find a Sun reference.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ian Clarke</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78120</link>
		<dc:creator>Ian Clarke</dc:creator>
		<pubDate>Tue, 22 Apr 2008 16:03:14 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78120</guid>
		<description>Immutability is generally regarded as a good thing in the functional programming world.  I wonder how much of a benefit this has from the perspective of helping the compiler to produce efficient code, has anyone tried benchmarking this?</description>
		<content:encoded><![CDATA[<p>Immutability is generally regarded as a good thing in the functional programming world.  I wonder how much of a benefit this has from the perspective of helping the compiler to produce efficient code, has anyone tried benchmarking this?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shams</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78115</link>
		<dc:creator>Shams</dc:creator>
		<pubDate>Tue, 22 Apr 2008 15:29:01 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78115</guid>
		<description>Not sure whether all variables should be final, especially places like null checks and reassignments. but in general I follow the same strategy of making parameters and local variables final.
Another strategy I follow to reduce typos is maintain a separate set of prefixes for member variables, parameters and local variables like m, p and e respectively</description>
		<content:encoded><![CDATA[<p>Not sure whether all variables should be final, especially places like null checks and reassignments. but in general I follow the same strategy of making parameters and local variables final.<br />
Another strategy I follow to reduce typos is maintain a separate set of prefixes for member variables, parameters and local variables like m, p and e respectively</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: matthew</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78101</link>
		<dc:creator>matthew</dc:creator>
		<pubDate>Tue, 22 Apr 2008 14:19:18 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78101</guid>
		<description>I make all variables and classes final.  I only make classes non-final if I am designing for inheritance.  I can&#039;t make method parameters final though, it just feels too noisy for me.

This makes me feel safe.  But I can only get into this mindset in Java.  I have yet to use the Object.freeze() method in Ruby, because it seems to be frowned upon.  I write my code and run my tests and everything seems right.</description>
		<content:encoded><![CDATA[<p>I make all variables and classes final.  I only make classes non-final if I am designing for inheritance.  I can&#8217;t make method parameters final though, it just feels too noisy for me.</p>
<p>This makes me feel safe.  But I can only get into this mindset in Java.  I have yet to use the Object.freeze() method in Ruby, because it seems to be frowned upon.  I write my code and run my tests and everything seems right.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John</title>
		<link>http://codemonkeyism.com/all-variables-in-java-must-be-final/comment-page-1/#comment-78057</link>
		<dc:creator>John</dc:creator>
		<pubDate>Tue, 22 Apr 2008 12:44:31 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/04/22/all-variables-in-java-must-be-final/#comment-78057</guid>
		<description>for (final int i = 0; ...</description>
		<content:encoded><![CDATA[<p>for (final int i = 0; &#8230;</p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced) (user agent is rejected)
Database Caching 7/105 queries in 0.181 seconds using disk

Served from: codemonkeyism.com @ 2010-07-30 12:39:45 -->