aspose file tools*
The moose likes Servlets and the fly likes Static Initialization Block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Static Initialization Block" Watch "Static Initialization Block" New topic
Author

Static Initialization Block

Arpit Gadle
Ranch Hand

Joined: Dec 16, 2008
Posts: 69


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


Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 433
    
    2
Why do you think the (every) servlet gets initialized when the web app / the server starts? Wouldn't an on-demand initializing more efficient?
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Arpit Gadle
Ranch Hand

Joined: Dec 16, 2008
Posts: 69

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

Joined: Apr 14, 2004
Posts: 10336


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

Joined: Dec 16, 2008
Posts: 69



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

Joined: Nov 04, 2009
Posts: 2066

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.
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 274

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.


SCJP 6 || SCWCD 5
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Static Initialization Block