Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static initialiser query

 
Sunetra Sen
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sunetra Sen
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sunetra Sen
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah it clears up the thing!
Thanks to both you guys.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic