Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!us.feeder.erje.net!news2.arglkargh.de!news.albasani.net!nuzba.szn.dk!pnx.dk!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Robert Klemme Newsgroups: comp.lang.java.programmer Subject: Usefulness of "final" (Was: Re: Inserting In a List) Date: Tue, 02 Apr 2013 21:06:25 +0200 Lines: 48 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 ngyunHZ4SDontKLH19vYvAYpvnYDoctuYnTiEiMejqQfP5TL8= Cancel-Lock: sha1:7GBgkfESeL1JkWtryf/8fb82FjY= User-Agent: Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/20130308 Thunderbird/17.0.4 In-Reply-To: Xref: csiph.com comp.lang.java.programmer:23188 On 04/02/2013 04:08 PM, lipska the kat wrote: > Just as a matter of interest what's with all the finals > > particularly > > for (final File name : folder.listFiles()) > > Despite initial appearances this is indeed legal as the > assignment is made multiple times but from the same statement. > > Given that the final keyword is, aside from a flag to the compiler for > possible optimization, largely documentary, what is the point of making > name final. It helps avoid accidental reassignment. And it helps distinguish unchanged from changed variables. > In fact what does peppering the code with finals do to make it easily > understandable to an inexperienced developer ? I believe in using "final" pretty often as it will immediately indicate which local variables are constant for a method call and which are modified all the time. Plus, with "final" you can easier catch errors in control flow: final String x; if ( someCondition() ) { x = y.toString(); } else { if ( someOtherCondition() ) { x = "foo"; } // forgot the else branch here x = "bar"; } System.out.println("We got " + x); Generally I find "finally" quite useful - apparently significantly more useful than you do. :-) Kind regards robert