GeeCON Prague 2014*
The moose likes Java in General and the fly likes How can I test to see if an object exists (yet)? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "How can I test to see if an object exists (yet)?" Watch "How can I test to see if an object exists (yet)?" New topic
Author

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

Gill Clover
Greenhorn

Joined: Aug 10, 2002
Posts: 28
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

Joined: Jan 30, 2002
Posts: 3451
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 ]

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Gill Clover
Greenhorn

Joined: Aug 10, 2002
Posts: 28
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

Joined: May 25, 2001
Posts: 388
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
 
jQuery in Action, 2nd edition
 
subject: How can I test to see if an object exists (yet)?