Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #7134

Re: Singleton Pattern

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.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 08:57:25 -0500
Date Tue, 16 Aug 2011 06:57:25 -0700
From Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com>
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> <vt-dnf5wc8CJkdrTnZ2dnUVZ_jKdnZ2d@posted.palinacquisition> <j2b8q2$6pq$1@dont-email.me> <d490d89a-2e63-4099-9629-c275f2cf9847@glegroupsg2000goo.googlegroups.com> <4o6dnfZWI_mOZtTTnZ2dnUVZ_oqdnZ2d@earthlink.com> <l4udnXOiVIQ2itfTnZ2dnUVZ_uKdnZ2d@posted.palinacquisition> <mpqdnaDwH__n8NfTnZ2dnUVZ_q6dnZ2d@earthlink.com>
In-Reply-To <mpqdnaDwH__n8NfTnZ2dnUVZ_q6dnZ2d@earthlink.com>
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
Message-ID <IbydnYJTAt_Y6tfTnZ2dnUVZ_vWdnZ2d@posted.palinacquisition> (permalink)
Lines 40
X-Usenet-Provider http://www.giganews.com
NNTP-Posting-Host 50.46.118.188
X-Trace sv3-up85BJ4uEl3h8ugQvmmDrPQ6PLJ6yipHtWVAaawo5JARdqnPEThW0fle5DCVg6n9kpiHoHZWMb7t6S3!tAv+E1OahD8/Wqytbiz42FwcZdi+WRdvSof1eqKkErMZ/rfDK7kn/gKYXrumQ+UNnujHIf5axe2E!p5QWNHoRjAPdHStpGpQ9pB0vPZ1N83pHtcIHZWzAOOc=
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 3601
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:7134

Show key headers only | View raw


On 8/16/11 6:15 AM, Patricia Shanahan wrote:
> [...]
>> 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.
>
> I read the last sentence as being a bit stronger than that. The JVM is
> required to delay class initialization until it is needed.

That's my read too.  And yes, that's even stronger assurance than what 
.NET provides.  So no real worries about premature initialization (see 
below).

> [...] Of course,
> the singleton could have another static method that gets called in a
> significant number of program runs that never call the getInstance
> method. If it does, it might be simpler and cleaner to refactor to
> change that in preference to jumping through hoops to avoid creating the
> instance during class initialization.

Exactly.  Such a situation is most likely to come about due to poor 
class design anyway, and so is not really a concern.  Much easier to 
just put the unrelated code that was potentially incurring the singleton 
initialization without using the singleton into a different class than 
to worry about actually writing code to defer singleton initialization 
explicitly.

Or looked at another way: if someone has messed up the class design such 
that a singleton class is used even though the singleton instance is 
never actually needed, I'm not sure I trust that person to correctly 
implement even the basic explicit singleton initialization 
synchronization code.  Even in that case, better to suffer the potential 
performance hit than to allow them to try to get the explicit 
initialization right.

So I would not be worried at all about using a field initializer to deal 
with singleton initialization.  It really is likely to be the best 
approach regardless of the situation, in all but the most unusual scenarios.

Pete

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Singleton Pattern "vbhavsar@gmail.com" <vbhavsar@gmail.com> - 2011-08-13 13:56 -0700
  Re: Singleton Pattern markspace <-@.> - 2011-08-13 14:41 -0700
  Re: Singleton Pattern Patricia Shanahan <pats@acm.org> - 2011-08-13 14:48 -0700
  Re: Singleton Pattern Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-08-13 16:43 -0700
    Re: Singleton Pattern Ian <m4r35n357@gmail.com> - 2011-08-15 15:00 +0100
      Re: Singleton Pattern Lew <lewbloch@gmail.com> - 2011-08-15 07:56 -0700
        Re: Singleton Pattern Patricia Shanahan <pats@acm.org> - 2011-08-15 22:07 -0700
          Re: Singleton Pattern Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-08-16 00:09 -0700
            Re: Singleton Pattern Patricia Shanahan <pats@acm.org> - 2011-08-16 06:15 -0700
              Re: Singleton Pattern Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-08-16 06:57 -0700
                Re: Singleton Pattern Patricia Shanahan <pats@acm.org> - 2011-08-16 09:25 -0700
                Re: Singleton Pattern Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-08-16 18:10 -0700
  Re: Singleton Pattern Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-08-13 20:56 -0400
    Re: Singleton Pattern Lew <lewbloch@gmail.com> - 2011-08-13 21:12 -0700
  Re: Singleton Pattern Rajeev <rajeev.nospam@gmail.com> - 2011-08-14 06:37 -0700

csiph-web