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


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

Re: Singleton Pattern

Date 2011-08-16 06:15 -0700
From Patricia Shanahan <pats@acm.org>
Newsgroups comp.lang.java.programmer
Subject Re: Singleton Pattern
References (1 earlier) <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>
Message-ID <mpqdnaDwH__n8NfTnZ2dnUVZ_q6dnZ2d@earthlink.com> (permalink)

Show all headers | View raw


On 8/16/2011 12:09 AM, Peter Duniho wrote:
> 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?

The Java specification says:

"Before a class is initialized, its direct superclass must be
initialized, but interfaces implemented by the class need not be
initialized. Similarly, the superinterfaces of an interface need not be
initialized before the interface is initialized.

A class or interface type T will be initialized immediately before the
first occurrence of any one of the following:

* T is a class and an instance of T is created.
* T is a class and a static method declared by T is invoked.
* A static field declared by T is assigned.
* A static field declared by T is used and the field is not a constant
variable (§4.12.4).
* T is a top-level class, and an assert statement (§14.10) lexically
nested within T is executed.

Invocation of certain reflective methods in class Class and in package
java.lang.reflect also causes class or interface initialization. A class
or interface will not be initialized under any other circumstance."

> 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. 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.

Patricia

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