Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can I test to see if an object exists (yet)?

 
Gill Clover
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
In my program I have made an instance of a properties object which gets loaded with the contents of a file (containing key/value pairs):
// read properties file
Properties properties = new Properties();
try {
properties.load(new FileInputStream("C:\\system.properties"));
...
Can I test to see if this properties object has been made already? I can potentially call the method that makes a properties instance several times and there's no need to have all these copies being made when just the one will suffice.
How would I do this?
Thanks,
Gillian Klee
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gillian,
There are at least a couple of things you could do.
First, if you define properties as an instance member in the class you can do this:

Or you could define properties as a static member and ininitialize it in a static block:


Michael Morris
[ March 02, 2003: Message edited by: Michael Morris ]
 
Gill Clover
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks...for some reason though it's not working still. Whenever the method is called that makes a properties instance, it always goes into the if(properties == null) condition.
It doesn't really matter though as my program works the way it is, I just thought it would be more efficient if only one properties instance was created.
Gillian
 
karl koch
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
then make the Properties an instance variable and create in the constructor if you want to have it once peer instance:


or make it static (as mentioned in the first answer).
k
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic