Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!nx02.iad01.newshosting.com!newshosting.com!216.196.98.146.MISMATCH!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.posted.palinacquisition!news.posted.palinacquisition.POSTED!not-for-mail NNTP-Posting-Date: Tue, 16 Aug 2011 02:09:31 -0500 Date: Tue, 16 Aug 2011 00:09:31 -0700 From: Peter Duniho User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.2.18) Gecko/20110616 Thunderbird/3.1.11 MIME-Version: 1.0 Newsgroups: comp.lang.java.programmer Subject: Re: Singleton Pattern References: <3be6e6cf-fa32-4503-9457-b0a1caef8f29@w11g2000vbp.googlegroups.com> <4o6dnfZWI_mOZtTTnZ2dnUVZ_oqdnZ2d@earthlink.com> In-Reply-To: <4o6dnfZWI_mOZtTTnZ2dnUVZ_oqdnZ2d@earthlink.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Message-ID: Lines: 27 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 50.46.118.188 X-Trace: sv3-n73Pt0J4tSeRhoQv/XulO+UNvhh+wIKoIhoMhJYs2X6HAWVcA9CTjaH1madfAzYWBDj12ZtS032s1QW!Q6PwvRB65p5C4LpyU15Kleuhk/U1xchpQzLrK8lD8faisNLd6vQmPxu6+cRZWUIs0cMvos9K4+ud!cSKkKcmTCn0xu2b/pqPKxmy2+XsDUiL02HKwYzQpHCo= X-Complaints-To: abuse@iinet.com X-DMCA-Complaints-To: abuse@iinet.com X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 2673 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:7132 On 8/15/11 10:07 PM, Patricia Shanahan wrote: > On 8/15/2011 7:56 AM, Lew wrote: > .... >> Final fields, especially immutable ones, make a lot of useful >> guarantees, and you don't have the field in the first place unless >> it's going to be used at some point anyway. Just freaking initialize >> it! > > Different program runs do different things, so there can be fields that > are used some runs but not others. That is especially the case for > library classes that may be used in many different programs. > > However, the initializer for a static field executes during class > initialization. How often does a singleton class get initialized without > its getInstance method ever being called? What does the Java specification say? In C#, the run-time is permitted to, and in fact does, delay class initialization until the class is actually accessed. I'd just always assumed Java took advantage of the same optimizations. If so, there is almost never any problem initializing singletons in a field initializer, because the classes are inherently not accessed unless you need the singleton. Pete