• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

static properties file

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my class I have declare a properties file with static:
private static Properties resource = new Properties();

I instantiate this class when my applet is loaded (init method) and in turn this class loads the properties file.

So far is this correct?

Now I wish to call myResourceClass.getProperty(key) from other classes in the applet.

If I simply import the ResourceClass can i resuse it without having to instantiate the class?

Is this correct?

Thanks
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You've declared the Properties field as private. That will prevent other classes from accessing it. You could provide a public method to access it that other classes could call. Or you could change it to public.
To access a static field in a class you code the classname followed by the field name: YourClass.resource would allow another class to get to the resource field (if its not private).

Not sure what you mean by "import the ResourceClass". Is that the class with the field resource? The import statement in a source program is there to help the compiler find a class definition when the file is being compiled. That's not related to instantiation which takes place at execution time.
Depending on the relationship between your classes, you may need the import statement to compile them when you use a reference to a static field as above.
After you compile the code, the one instantiation that sets the static field will be all that's needed. Other classes will be able to use it.
 
Robert Kennedy
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. Your reply clears it up.
 
reply
    Bookmark Topic Watch Topic
  • New Topic