Patricia Shanahan wrote:
Make rootDir private and change it only once inside the class, from main.
If other classes need access you can provide a getRootDir() method,
which can check for attempts to read it before it has been set.
Patricia
Suppose my program has about 20 classes and the class with main method
is MyClass provided in my original posting. Suppose one class
"Worker.java" needs to know the root directory in order to read in the
text file. Right now, Worker does NOT have a MyClass in it. But, if I
follow your method, I need:
<inside Worker.java>
MyClass class = new MyClass();
String rootDir = class.getRootDir();
...//append rootDir in the front of text file name
</inside Worker.java>
I feel it is kind of silly that in order to get to know what the root
directory it is, Worker is forced to have a MyClass in it.
pass them in arguments or use a singleton. In the end it is probably
other classes. People are going to shudder though so just don't tell
them that you are doing that.