Path: csiph.com!usenet.pasdenom.info!gegeweb.org!eternal-september.org!feeder.eternal-september.org!mx05.eternal-september.org!.POSTED!not-for-mail From: markspace Newsgroups: comp.lang.java.programmer Subject: Re: Usefulness of "final" (Was: Re: Inserting In a List) Date: Sun, 07 Apr 2013 09:02:57 -0700 Organization: A noiseless patient Spider Lines: 26 Message-ID: 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> <515cde06$0$32111$14726298@news.sunsite.dk> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sun, 7 Apr 2013 16:00:26 +0000 (UTC) Injection-Info: mx05.eternal-september.org; posting-host="fba3415ba68d85d643935af2f52f0b4b"; logging-data="17995"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+wFaaBbgVb+/h5IM/wOqtlVdXibnag3RI=" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130328 Thunderbird/17.0.5 In-Reply-To: Cancel-Lock: sha1:KMKfF77f/IL69sYNoepRUQ6W57o= Xref: csiph.com comp.lang.java.programmer:23366 On 4/7/2013 8:28 AM, Wanja Gayk wrote: > public class Foo{ > private int value; > private Foo(int value){this.value=value;} > public static Foo createFoo(int value){return new Foo(value);} > public int getValue(){return value;} > } > > ..is both immutable, thread safe and can't be overridden either, without No, he doesn't say that, and no, this class isn't thread safe. Normal POJO classes aren't thread safe. Adding a factory method doesn't help. In order for this class to be thread safe 'value' would have to be made visible somehow. You need volatile, a synchronized block, or something similar. Please read the JLS (and JCIP, again) and note section 17 Threads and Locks. The whole point of that section is that regular writes aren't thread safe. If you'd like to point out where you think JCIP or the JLS says that the above class is thread safe, I'll be happy to show what sections I think say differently. At worst, one of us will learn something. ;-)