File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static properties file

 
Robert Kennedy
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Norm Radder
Ranch Hand
Posts: 707
3
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. Your reply clears it up.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic