File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Applets and the fly likes static properties file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "static properties file" Watch "static properties file" New topic

static properties file

Robert Kennedy
Ranch Hand

Joined: Jun 27, 2008
Posts: 63
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?

Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 696
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

Joined: Jun 27, 2008
Posts: 63
Thank you. Your reply clears it up.
I agree. Here's the link:
subject: static properties file
It's not a secret anymore!