Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!news.netfront.net!not-for-mail From: Wanja Gayk Newsgroups: comp.lang.java.programmer Subject: Re: Usefulness of "final" (Was: Re: Inserting In a List) Date: Sun, 7 Apr 2013 19:20:22 +0200 Organization: Netfront http://www.netfront.net/ Lines: 47 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> NNTP-Posting-Host: 77.176.242.136 Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Trace: adenine.netfront.net 1365355075 37123 77.176.242.136 (7 Apr 2013 17:17:55 GMT) X-Complaints-To: news@netfront.net NNTP-Posting-Date: Sun, 7 Apr 2013 17:17:55 +0000 (UTC) User-Agent: MicroPlanet-Gravity/3.0.4 Xref: csiph.com comp.lang.java.programmer:23367 In article , markspace (markspace@nospam.nospam) says... > > 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. You're right in that regard, in rare cases a second thread could see the "0" default. But you know it's check mate in 1 draw, my friend. Just watch this: public class Foo{ private volatile int value; private Foo(int value){this.value=value;} public static Foo createFoo(int value){return new Foo(value);} public int getValue(){return value;} } Now that's immutable and thread safe and still there is no "final" used. Check mate! Kind regards, -Wanja- -- ..Alesi's problem was that the back of the car was jumping up and down dangerously - and I can assure you from having been teammate to Jean Alesi and knowing what kind of cars that he can pull up with, when Jean Alesi says that a car is dangerous - it is. [Jonathan Palmer] --- news://freenews.netfront.net/ - complaints: news@netfront.net ---