• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Static Initialization Block

 
Ranch Hand
Posts: 69
MySQL Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Static Initialization Block is executed when the class is loaded and before creation of any object.

I have a doubt

When a web server starts, a single object of servlet is created and initialized. So the static initialization block should get executed
when the web server starts. Right?

But what i observed in sample web-app is that the static initialization block is executed when servlet is accessed for the first time.

Please clear my doubt....

Regards,
Arpit


 
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you think the (every) servlet gets initialized when the web app / the server starts? Wouldn't an on-demand initializing more efficient?
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's upon the servlet container! Check the Spec!

SRV.2.3.1 Loading and Instantiation
The servlet container is responsible for loading and instantiating servlets. The loading and instantiation can occur when the container is started, or delayed until the container determines the servlet is needed to service a request

 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Servlets are only loaded at startup if they are configured to do so. See the <load-on-startup /> element in web.xml. Otherwise the section of the specification quoted above defines happens.
 
Arpit Gadle
Ranch Hand
Posts: 69
MySQL Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Say using <load-on-startup>1</load-on-startup> won't load & instantiate a servlet if the servlet container is configured to load it on startup.

Abimaran Kugathasan wrote:

SRV.2.3.1 Loading and Instantiation
The servlet container is responsible for loading and instantiating servlets. The loading and instantiation can occur when the container is started, or delayed until the container determines the servlet is needed to service a request



Can we change this servlet container setting....

 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Say using <load-on-startup>1</load-on-startup> won't load & instantiate a servlet if the servlet container is configured to load it on startup.


What? The specification says a Servlet container should:


Instantiate an instance of each servlet identified by a <servlet> element that
includes a <load-on-startup> element in the order defined by the load-on-startup
element values, and call each servlet instance’s init() method.


 
Arpit Gadle
Ranch Hand
Posts: 69
MySQL Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Sorry for mistyping..

What I really mean is

Say using <load-on-startup>1</load-on-startup> won't load & instantiate a servlet if the servlet container is not configured to load it on startup.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Arpit Gadle wrote:
Say using <load-on-startup>1</load-on-startup> won't load & instantiate a servlet if the servlet container is not configured to load it on startup.


This is what Spec says!

The load-on-startup element indicates that this servlet should be loaded (instantiated and have its init() called) on the startup of the web application. The optional contents of these element must be an integer indicating the order in which the servlet should be loaded. If the value is a negative integer, or the element is not present, the container is free to load the servlet whenever it chooses. If the value is a positive integer or 0, the container must load and initialize the servlet as the application is deployed. The container must guarantee that servlets marked with lower integers are loaded before servlets marked with higher integers. The container may choose the order of loading of servlets with the same load-on-start-up value.

 
Ranch Hand
Posts: 282
Eclipse IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that you're misunderstanding the way static initializers work. It is when the class is first accessed that the static initializer blocks is executed and all the static variables are set, not when the Java program starts.

For example, in this test program, the string "static init block" isn't printed until a new instance of Test2 is created.


So in the case of servlets, because servlets are, by default, instantiated when they are first requested, it makes sense that its static init block isn't called until then.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic