Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsfeed.utanet.at!newsfeed2.utanet.at!newscore.univie.ac.at!aconews-feed.univie.ac.at!aconews.univie.ac.at!not-for-mail Newsgroups: comp.lang.java.programmer From: Andreas Leitgeb Subject: Re: Style Police (a rant) References: Reply-To: avl@logic.at User-Agent: slrn/pre0.9.9-111 (Linux) Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-ID: Date: 17 Sep 2011 19:56:21 GMT Lines: 36 NNTP-Posting-Host: gamma.logic.tuwien.ac.at X-Trace: 1316289381 tunews.univie.ac.at 11354 128.130.175.3 X-Complaints-To: abuse@tuwien.ac.at Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:8109 Wanja Gayk wrote: > avl@gamma.logic.tuwien.ac.at says... >> While a *non*-final List typed variable could later be >> assigned an instance of LinkedList, a final can't. >> It hardly ever hurts to have a final variable carry the most >> specific subtype. > final x = new Vector(); > Enumeration e = x.elements(); > [ later someone changes it to ] > final x = new LinkedList(); > Enumeration e = x.elements(); <<- and breaks this and [...] I think it's even a bonus that this would no longer compile. On the other hand: final List l = new Vector(); [...] // I know, I initialized l with a Vector just before: Vector vec=(Vector)l; // compiler cannot check Enumeration e = vec.elements(); wouldn't crash until Runtime, if l's initializer were changed to LinkedList lateron... > Abstraction is the whole point of declaring interfaces, you don't want > your code to be brittle. But inconsequent abstraction is even much worse than just forgetting about it altogether. PS: Obviously, one cannot just idly change the initializer of a variable and expect it all to work. Not even without the currently discussed feature.