Static and Instantiation

Discussion in 'Java' started by Jason Cavett, Oct 6, 2008.

  1. Jason Cavett

    Jason Cavett Guest

    If I make a static class (AKA - everything in a class is static), does
    that mean this class is loaded into memory as soon as the JVM starts
    (assuming the class is being used somewhere within the application),
    or is it lazily instantiated and not loaded into memory until it
    actually needs to be used?

    I searched around, but couldn't find the information. Thanks.
     
    Jason Cavett, Oct 6, 2008
    #1
    1. Advertising

  2. Jason Cavett wrote:
    > If I make a static class (AKA - everything in a class is static), does
    > that mean this class is loaded into memory as soon as the JVM starts
    > (assuming the class is being used somewhere within the application),
    > or is it lazily instantiated and not loaded into memory until it
    > actually needs to be used?
    >
    > I searched around, but couldn't find the information. Thanks.


    Class files are loaded on demand.

    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
     
    Joshua Cranmer, Oct 6, 2008
    #2
    1. Advertising

  3. Jason Cavett

    Lew Guest

    On Oct 6, 1:19 pm, Joshua Cranmer <> wrote:
    > Jason Cavett wrote:
    > > If I make a static class (AKA - everything in a class is static), does
    > > that mean this class is loaded into memory as soon as the JVM starts
    > > (assuming the class is being used somewhere within the application),
    > > or is it lazily instantiated and not loaded into memory until it
    > > actually needs to be used?

    >
    > > I searched around, but couldn't find the information.  Thanks.

    >
    > Class files are loaded on demand.


    And initialized somewhat later.

    OP: Check out the JLS.
    <http://java.sun.com/docs/books/jls/third_edition/html/
    execution.html#12.2.>
    <http://java.sun.com/docs/books/jls/third_edition/html/
    execution.html#12.4>

    One of the more interesting consequences of these rules is that you
    can refer to the 'class' literal (as in 'SomeClass.class') without
    initializing the class. This has consequence for reflection.

    --
    Lew
     
    Lew, Oct 6, 2008
    #3
  4. Jason Cavett

    Mark Space Guest

    Jason Cavett wrote:
    > If I make a static class (AKA - everything in a class is static), does
    > that mean this class is loaded into memory as soon as the JVM starts
    > (assuming the class is being used somewhere within the application),
    > or is it lazily instantiated and not loaded into memory until it
    > actually needs to be used?
    >
    > I searched around, but couldn't find the information. Thanks.



    Probably the lazy instantiated one, but if it's critical you may not
    want to count on it.

    The default JVM loader does not load or initialize classes until needed,
    but if your class goes through a JIT optimizer, your application just
    might get all initialized and loaded right at start up, depending on how
    the JIT compiler "sees" your code.

    The best way to do this would probably be to count on lazy instantiation
    (it'll work 99% of the time) but leave enough instrumentation in the
    code that you can tell if it ever doesn't happen. In other words,
    collect data from the running program. If you see performance drop and
    the static class is the issue, you can tackle the problem then.

    I'd log the static init procedure, and log just before you use it. So,
    if you see the class getting initialized, but never see the "use" log
    message, something happened to the lazy init feature.
     
    Mark Space, Oct 6, 2008
    #4
  5. Jason Cavett

    Jason Cavett Guest

    On Oct 6, 2:33 pm, Mark Space <> wrote:
    > Jason Cavett wrote:
    > > If I make a static class (AKA - everything in a class is static), does
    > > that mean this class is loaded into memory as soon as the JVM starts
    > > (assuming the class is being used somewhere within the application),
    > > or is it lazily instantiated and not loaded into memory until it
    > > actually needs to be used?

    >
    > > I searched around, but couldn't find the information.  Thanks.

    >
    > Probably the lazy instantiated one, but if it's critical you may not
    > want to count on it.
    >
    > The default JVM loader does not load or initialize classes until needed,
    > but if your class goes through a JIT optimizer, your application just
    > might get all initialized and loaded right at start up, depending on how
    > the JIT compiler "sees" your code.
    >
    > The best way to do this would probably be to count on lazy instantiation
    > (it'll work 99% of the time) but leave enough instrumentation in the
    > code that you can tell if it ever doesn't happen.  In other words,
    > collect data from the running program.  If you see performance drop and
    > the static class is the issue, you can tackle the problem then.
    >
    > I'd log the static init procedure, and log just before you use it.  So,
    > if you see the class getting initialized, but never see the "use" log
    > message, something happened to the lazy init feature.


    Alright. Actually, the laziness of instantiation isn't the problem.
    It's actually the reverse. I don't want everything loaded.

    The reason for this is I'm developing a Java library for some other
    applications, and it is rather large. I wanted to make sure that the
    few classes that I made static wouldn't get loaded just by the sheer
    fact that someone was using another component in the library that had
    nothing to do with that class.

    Intuition told me that it wouldn't make sense to load all the static
    "stuff" up front (although, good point about the JIT compilers), but I
    wanted to check to make sure.

    Thanks for the response.
     
    Jason Cavett, Oct 6, 2008
    #5
    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. Neil Zanella
    Replies:
    1
    Views:
    497
    Jonathan Bromley
    Oct 17, 2003
  2. Anand P Paralkar

    NCVHDL/NCELAB and Recursive Instantiation

    Anand P Paralkar, Nov 10, 2003, in forum: VHDL
    Replies:
    2
    Views:
    3,110
    Mike Treseler
    Nov 10, 2003
  3. Erik Arner
    Replies:
    0
    Views:
    434
    Erik Arner
    Nov 23, 2004
  4. Replies:
    1
    Views:
    598
    Salt_Peter
    Dec 25, 2006
  5. o_ssie
    Replies:
    0
    Views:
    329
    o_ssie
    Apr 9, 2009
Loading...

Share This Page