static variables in web applications versus standalone application

Discussion in 'Java' started by Matt, Nov 13, 2004.

  1. Matt

    Matt Guest

    When we declare static variable in a standalone application,
    the lifetime of a static variable begins from the execution of the program
    untill the termination of the program.

    When we declare static variable in a web application,
    the lifetime of a static variable begins from the the start of the web application
    in web server, untill the web application is stopped in web server.
    At first, I thought the lifetime of a static variable in a web application is
    from opening the web browser until the web browser closes, that is session
    lifetime, and now I realize it is wrong.

    Please confirm the concept. Thanks
    Matt, Nov 13, 2004
    #1
    1. Advertising

  2. Matt

    Tony Morris Guest

    "Matt" <> wrote in message
    news:...
    > When we declare static variable in a standalone application,
    > the lifetime of a static variable begins from the execution of the program
    > untill the termination of the program.
    >
    > When we declare static variable in a web application,
    > the lifetime of a static variable begins from the the start of the web

    application
    > in web server, untill the web application is stopped in web server.
    > At first, I thought the lifetime of a static variable in a web application

    is
    > from opening the web browser until the web browser closes, that is session
    > lifetime, and now I realize it is wrong.
    >
    > Please confirm the concept. Thanks


    The 'lifetime' of a static member is the same as the class that encapsulates
    it.
    The 'lifetime' of a class is from the moment it is loaded from the time it
    is unloaded.
    A class is loaded "on-demand" as per VM Specification.
    A class is unloaded when the VM system class loader cache dies, which is at
    VM exit time.

    A session variable is explained in the J2EE tutorial.
    Note that it is not associated with a VM, class or class loader in the same
    way a static field is.
    It is also superficial in that it has nothing to do with your web browser -
    it can be thought of as being associated with one or more requests, which is
    independant of a browser.

    Time to do some reading.

    --
    Tony Morris
    http://xdweb.net/~dibblego/
    Tony Morris, Nov 13, 2004
    #2
    1. Advertising

  3. Matt

    Guest

    The static variable dies in a standalone application, because the
    garbage collector cleans all your object because you application has
    ended.

    But if you are using this in a servlet or jsp, your application is not
    dead until your web server dies. So even if you closes the web browser,
    your web server is not close. Try to shut down your tomcat and you
    variable will be no more.

    hope this help
    , Nov 13, 2004
    #3
  4. "Tony Morris" <> wrote in message
    news:L2dld.34074$...
    > "Matt" <> wrote in message
    > news:...
    >> When we declare static variable in a standalone application,
    >> the lifetime of a static variable begins from the execution of the
    >> program
    >> untill the termination of the program.
    >>
    >> When we declare static variable in a web application,
    >> the lifetime of a static variable begins from the the start of the web

    > application
    >> in web server, untill the web application is stopped in web server.
    >> At first, I thought the lifetime of a static variable in a web
    >> application

    > is
    >> from opening the web browser until the web browser closes, that is
    >> session
    >> lifetime, and now I realize it is wrong.
    >>
    >> Please confirm the concept. Thanks

    >
    > The 'lifetime' of a static member is the same as the class that
    > encapsulates
    > it.
    > The 'lifetime' of a class is from the moment it is loaded from the time it
    > is unloaded.
    > A class is loaded "on-demand" as per VM Specification.
    > A class is unloaded when the VM system class loader cache dies, which is
    > at
    > VM exit time.


    A class is unloaded when the classloader which loaded it is unloaded. If
    this is a web-application-specific classloader, this may happen when the
    application exits or sometime thereafter. Ot it may not, depending both on
    the VM implementation and on the container's classloader implementation.
    Mike Schilling, Nov 13, 2004
    #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. John Davis
    Replies:
    0
    Views:
    716
    John Davis
    Aug 21, 2003
  2. Matthew Louden
    Replies:
    1
    Views:
    6,873
    Scott M.
    Oct 11, 2003
  3. Battler
    Replies:
    2
    Views:
    804
    Chris Smith
    Aug 18, 2004
  4. John Davis
    Replies:
    0
    Views:
    350
    John Davis
    Aug 21, 2003
  5. Paul Butcher
    Replies:
    12
    Views:
    685
    Gary Wright
    Nov 28, 2007
Loading...

Share This Page