X-Received: by 10.224.157.1 with SMTP id z1mr2274323qaw.8.1365039081916; Wed, 03 Apr 2013 18:31:21 -0700 (PDT) X-Received: by 10.49.37.39 with SMTP id v7mr423301qej.27.1365039081901; Wed, 03 Apr 2013 18:31:21 -0700 (PDT) Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!ca1no28403674qab.0!news-out.google.com!ef9ni1849qab.0!nntp.google.com!ca1no28403669qab.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.java.programmer Date: Wed, 3 Apr 2013 18:31:21 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=69.28.149.29; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T NNTP-Posting-Host: 69.28.149.29 References: <19un43xj77bua.vw45l4e2wshi.dlg@40tude.net> <515cabb2$0$32111$14726298@news.sunsite.dk> <515cc192$0$32109$14726298@news.sunsite.dk> <515cc3b2$0$32111$14726298@news.sunsite.dk> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: Re: Usefulness of "final" (Was: Re: Inserting In a List) From: Lew Injection-Date: Thu, 04 Apr 2013 01:31:21 +0000 Content-Type: text/plain; charset=ISO-8859-1 Xref: csiph.com comp.lang.java.programmer:23267 markspace wrote: > final is necessary. It is not sufficient (you also have to not mutate > the object in question after the enclosing object's ctor exits). I'm > pretty sure the field must also be explicitly private. (Implicitly Nope. You can have immutable public fields. > might not be enough to satisfy what ever escape analysis the Java > compiler does.) 'final' is essential to the definition of constant variables, which do have special initialization semantics. 'final' is an essential piece in locking down an immutable reference variable of a type; call this type 'ImmutableA'. The object referenced by the member is immutable by dint of its implementation, which we'll postulate to be correct. public class ImmutableA { private String ident = "Immutable instance"; } While the object referenced by 'ident' is immutable, this is not enough to make instances of 'ImmutableA' immutable. For that you must have 'final'. public class ImmutableA { private final String ident = "Immutable instance"; } Now 'ImmutableA' instances are immutable. Not only that, but in this case, because 'ident' is 'final' and initialized by a constant expression, it is now a constant variable. So 'final' is most assuredly involved in creating immutability, and is more reliable in the face of refactoring and other maintenance to preserve it than immutability idioms that do not use 'final'. -- Lew