Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

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

 
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
 
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
 
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
 
Hey! Wanna see my flashlight? It looks like this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic