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?
Joined: Aug 10, 2005
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.