When gets the static initializer of a private static class exectuted?

Discussion in 'Java' started by joes, Jan 9, 2008.

  1. joes

    joes Guest

    Hello Gurus

    I have a question. I have a class which holds an inner class declared
    "private static". This inner class has a static field which is
    immediately initilaized. When gets this static initilaziation of a
    private static class exectuted?

    - does it get executed when the outer class is loaded?
    - does it get only executed if the inner class is getting loaded?


    Example:
    ------------
    public class Log {


    private static class LoggerHolder {
    public static Logger sLogger =
    LoggerHelper.getLogger(LOGGER_CLASS);
    }

    public static Logger getLogger() {
    return LoggerHolder.sLogger;
    }
    }

    many thanks
    regards

    Mark
    joes, Jan 9, 2008
    #1
    1. Advertising

  2. joes wrote:
    > Hello Gurus
    >
    > I have a question. I have a class which holds an inner class
    > declared
    > "private static". This inner class


    Nitpick. It's not an inner class. Inner classes may not have static
    fields. It's a "nested" class.

    > has a static field which is
    > immediately initilaized. When gets this static initilaziation of a
    > private static class exectuted?
    >
    > - does it get executed when the outer class is loaded?
    > - does it get only executed if the inner class is getting loaded?


    Only when the class which defines the static field gets loaded.
    Mike Schilling, Jan 9, 2008
    #2
    1. Advertising

  3. joes

    Roedy Green Guest

    On Tue, 8 Jan 2008 23:38:15 -0800 (PST), joes <> wrote,
    quoted or indirectly quoted someone who said :

    >
    >- does it get executed when the outer class is loaded?
    >- does it get only executed if the inner class is getting loaded?


    Insert System.out.println( "inner(outer) static loading")
    and tell us what you discover.
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Jan 9, 2008
    #3
  4. joes

    Lew Guest

    joes wrote:
    >> I have a question. I have a class which holds an inner class
    >> declared
    >> "private static". This inner class


    Mike Schilling wrote:
    > Nitpick. It's not an inner class. Inner classes may not have static
    > fields. It's a "nested" class.


    >> has a static field which is
    >> immediately initilaized. When gets this static initilaziation of a
    >> private static class exectuted?
    >>
    >> - does it get executed when the outer class is loaded?
    >> - does it get only executed if the inner class is getting loaded?


    > Only when the class which defines the static field gets loaded.


    Only when the first object of the nested class type is used. This is the
    basis for a thread-safe lazy-loading idiom much ballyhooed by Brian Goetz, et al.
    <http://www-128.ibm.com/developerworks/java/library/j-jtp03304/>
    Within the outer class:
    > private static class LazySomethingHolder {
    > public static Something something = new Something();
    > }
    >
    > ...
    >
    > public static Something getInstance() {
    > return LazySomethingHolder.something;
    > }


    The nested class's static initializer is not run when the outer class is
    loaded but when the getInstance() method is first called.

    --
    Lew
    Lew, Jan 9, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DaveLessnau
    Replies:
    3
    Views:
    428
    Howard
    May 16, 2005
  2. Marvin Barley
    Replies:
    6
    Views:
    396
    Marvin Barley
    Feb 19, 2007
  3. chucky
    Replies:
    14
    Views:
    1,285
    Twisted
    Aug 2, 2007
  4. Andreas Wollschlaeger

    Mimicking Javas static class initializer in C++

    Andreas Wollschlaeger, Oct 27, 2008, in forum: C++
    Replies:
    8
    Views:
    454
    Richard Herring
    Oct 29, 2008
  5. Hicham Mouline
    Replies:
    5
    Views:
    2,366
    James Kanze
    Dec 19, 2008
Loading...

Share This Page