Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: markspace <-@.> Newsgroups: comp.lang.java.programmer Subject: Re: Synchronization of the constructor Date: Sun, 14 Aug 2011 07:51:48 -0700 Organization: A noiseless patient Spider Lines: 52 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sun, 14 Aug 2011 14:51:57 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="XjIWM99mD7Ijfdu600oVPA"; logging-data="16912"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18ejg3KPJbg054rhRCFaUj+AQTLJ66JhrA=" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20110624 Thunderbird/5.0 In-Reply-To: Cancel-Lock: sha1:btixHZ4JpcnzXO6n0GZD6mvsqFI= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:7108 On 8/14/2011 7:42 AM, markspace wrote: > public class Stooges { > > private final ArrayList stooges; > > public Stooges() { > ArrayList temp = new ArrayList(3); > temp.add( "Larry" ); > temp.add( "Moe" ); > temp.add( "Curly" ); > stooges = temp; > } > public List getStooges() { > return new ArrayList( stooges ); > } > } I want to make one small change here. The above is correct, but misleading. Both of the following are also immutable and thread safe: public class Stooges { private final ArrayList stooges; public Stooges() { ArrayList stooges = new ArrayList(3); stooges.add( "Larry" ); stooges.add( "Moe" ); stooges.add( "Curly" ); } public List getStooges() { return new ArrayList( stooges ); } } -- or -- public class Stooges { private final ArrayList stooges = new ArrayList(3); public Stooges() { stooges.add( "Larry" ); stooges.add( "Moe" ); stooges.add( "Curly" ); } public List getStooges() { return new ArrayList( stooges ); } }