Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news-1.dfn.de!news.dfn.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Robert Klemme Newsgroups: comp.lang.java.programmer Subject: Re: Usefulness of "final" (Was: Re: Inserting In a List) Date: Wed, 03 Apr 2013 23:05:57 +0200 Lines: 65 Message-ID: References: <19un43xj77bua.vw45l4e2wshi.dlg@40tude.net> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net 9PDehoMkU0//p9bH+wOj+A4jRMY1YfbTgzrGmbl6JV3scvSRA= Cancel-Lock: sha1:Rc3BKis39s0vjTfRjzmSUmV6cD4= User-Agent: Mozilla/5.0 (Windows NT 6.0; WOW64; rv:17.0) Gecko/20130328 Thunderbird/17.0.5 In-Reply-To: Xref: csiph.com comp.lang.java.programmer:23224 On 03.04.2013 20:51, lipska the kat wrote: > On 03/04/13 18:08, Robert Klemme wrote: >> I am not sure what you mean by that. Can you elaborate? Where's the >> storage class in the example above? > > final, although it's not is it, at least it's not Java terminology, > apologies, I should have said 'modifier'. I'll restate. Ah, OK, I see. Thanks! > Well I'm not sure that using a modifier to help you write a > conditional statement is 'good programming style'. When I see the > modifier final it says something to me, it says, this value is not > modifiable ('scuse the pun). Sometimes you need nested conditions as shown and with a blank final you can ensure that the variable is assigned exactly once on every possible path. I find that useful. > Is it improving the clarity of your code to > use final for it's side effect, that is the side effect of causing the > compiler to barf because a final variable may already have been > initialized. I'm not sure about that. That's not a side effect - it is _the_ effect of "final" modifier for variables. "final" ensures a variable is assigned to at most once: http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.12.4 There is no other effect - final is all about assignment. This is true for local variables - it's different with field which get assigned compile time constants. I'd say, "final" is probably even more useful for fields because it ensures that a field is initialized in every constructor path - you cannot forget it. http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.3.1.2 > For a single local variable I'd probably agree, in fact in general I > would agree but that wasn't my initial point really, in the code that > kicked off this sub thread there was more than one final variable, in > fact there were several in close proximity, I was initially questioning > the clarity of this for a new user. However then I opened my mouth and > put my foot in it and said ... :-) >> Maybe I could better see (and agree) if you provide a specific example >> of what you mean here. > > I think you probably know what I mean and any off the cuff example will > be contrived to the point irrelevance, so, leave it with me and I'll see > if I can come up with a simple self contained example. Thank you! Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/