Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

java.util.properties - static  RSS feed

 
Vik Natarajan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to load the properties for a class from a properties file just once and avoid the File I/O everytime this class is instantiated?
How would I do this using static members?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure what you mean. You seem to be implying some circumstance where the JVM reads "the properties for a class" automatically, and you'd like it to happen less frequently. If it's under your control, of course, then you simply have to only do it once. Storing the result in a static variable makes perfect sense. If there's more to your question, can you give more details?
 
Vik Natarajan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick reply.
Let me try to explain in detail
What I am trying to do is read values from a properties file for this class.
What I am trying to avoid is to read from the properties file to get the values every time this class is instantiated, so that I can avoid the file I/O.
My confusion is where to place the FileInputStream statement
FileInputStream thisfile = new FileInputStream("something.props");
Do I place the properties setup code in the constructor? but this would get executed every time an object of this class is instantiated.
But If I set the values(from the props file) of the static members the first time an object of the class is instantiated, is there a way I dont have to read the file over and over again for every instance.
I hope this explains it better.
[ July 30, 2003: Message edited by: Vik Natarajan ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, OK. So what you probably want to do is called "lazy initialization;" something like this:

If you omit the "synchronized" block, this will still work, although it's then possible that the properties could be loaded more than once.
 
Vik Natarajan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much, this will limit the number of I/Os. Even though this will not ensure only one occurance of the I/O.
Thanks again.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With the synchronized it's guaranteed to happen only once. Without the synchronized it might happen more than once depending on how often the object is constructed. If you have 100 or so being constructed at exactly the same time then you might have a couple of them reading the file before you set it from null.
 
Vik Natarajan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest and Thomas, my class is ready now and good to go.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!