Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes static initialiser query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "static initialiser query" Watch "static initialiser query" New topic
Author

static initialiser query

Sunetra Sen
Ranch Hand

Joined: Feb 17, 2005
Posts: 43
Hi,
I have a Thread class(implements Runnable) which requires to read some values from the application config file and use them in the run() method.
Now these values are for all purposes constants.
So I am thinking of reading these in a static block in the tread class so that these get initialised during class loading.
Is this idea correct..when is a class in java unloaded?
Will declaring in a static block ensure that these values are read only and only once from the config?
Please clarify!
Thanks in advance,
Sunetra.
Nischal Tanna
Ranch Hand

Joined: Aug 19, 2003
Posts: 182
A class in java is loaded when the first request for its reference occurs ( for a single class loader). Thus, more often a class is loaded at runtime. static initialisers(blocks) are executed when a class is loaded (first request).

A class is unloaded when the server is restarted.

Thus, its a guarantee for you that the config file will be read only once in the server lifetime.


Thnx
Sunetra Sen
Ranch Hand

Joined: Feb 17, 2005
Posts: 43
Thanks Nischal!
just one more doubt:
suppose we want to import the same class(say class A) in two other classes(say class B and class C).
Now if we know for sure that class B will be initialised(and hence loaded) before class C,then can we omit the import statement(for importing A) in class C?
(Because during loading of class B the class A was already loaded with the import statement in class B.)
My understanding is that the import statements loads the class(or classes if we import a package).
Is this correct? :roll:

Thanks again,
Sunetra.
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8903

Originally posted by Sunetra Sen:
Thanks Nischal!
just one more doubt:
suppose we want to import the same class(say class A) in two other classes(say class B and class C).
Now if we know for sure that class B will be initialised(and hence loaded) before class C,then can we omit the import statement(for importing A) in class C?
(Because during loading of class B the class A was already loaded with the import statement in class B.)
My understanding is that the import statements loads the class(or classes if we import a package).
Is this correct? :roll:

Thanks again,
Sunetra.


Import statements have nothing to do with the loading of a class.If a class 'x' is used in the code then import is required unless both are in the same package. Hope this helps.


Groovy
Sunetra Sen
Ranch Hand

Joined: Feb 17, 2005
Posts: 43
Yeah it clears up the thing!
Thanks to both you guys.
 
 
subject: static initialiser query
 
Similar Threads
Singletons in servlets - threadsafe?
static final variables initilization in EJBs
Question Regarding class loading precedence.
Initialization for a JSP
Static Block in JSPs