• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is it a best practice to read an xml file in a static block  RSS feed

 
Sajith Hasanka
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

i have a requirement to read an xml configuration file., and access the elements from time to time.

What i would think is to read the whole file in the program startup.

Is it ok to use a static block to read the xml file and load at the startup ?

Thanks
 
Manoj Kumar Jain
Ranch Hand
Posts: 198
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If we need some variables frequently through out the execution of the program then I think its better to read them once and keep in memory, because reading the properties file each time might be expensive.
you can store them as a static variables of a class.

does it really make difference if you are read them in a static block or in main method ?? I am not sure.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think that would be a good practice in general. If you do it to all kinds of initialization, you might end up with circular dependencies of classes and have to refactor it anyway. Use a method (static, if need be) which will be explicitly called. This way the order of initialization procedures in your project can be more easily controlled and checked.

However, this is no easy question: getting the initialization of a more complicated system right can be surprisingly hard, especially if you also need a clean shutdown. Others might have different experiences and opinions, though.

Edit: I've only answered the question "Is it ok to use a static block to read the xml file and load at the startup?". The rest seems good to me.
 
Manoj Kumar Jain
Ranch Hand
Posts: 198
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Martin Vajsar wrote:I don't think that would be a good practice in general. If you do it to all kinds of initialization, you might end up with circular dependencies of classes and have to refactor it anyway. Use a method (static, if need be) which will be explicitly called. This way the order of initialization procedures in your project can be more easily controlled and checked


Yes, If there is initialization in static block across many classes then it leads to confusion and can't be check easily. It's better to call a method that explicitly initialize the things in proper sequence as desired.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!