I’m adapting a simulation written in Java. My limited background is all in C++.
The current implementation of the simulation relies on a class called Parameters. An instance of class Simulation references attributes of Parameters, which I don’t think is ever instantiated. The Parameters class has a structure like
public class Parameters {
public static int day = 0;
public static final int endDay = 365;
....
public static int getDate() {
return day;
}
}
In an instance of Simulation, there are references to, e.g., Parameters.day.
Currently, all the attributes of Parameters are hard-coded. I need to be able to change some of them with command-line arguments. For example, I’d like to be able to set a different endDay using a Parameters::setEndDay(int customDay) sort of function.
My first thought was to create an instance (Parameters parameters = new Parameters()) and completely rewrite the Parameters class so that all its attributes are private and only accessible through accessor functions. I’m concerned that this approach is not very efficient. Thus far, I have tried a hybrid approach in which I create an instance of the Parameters class, which I then pass to an instance of Simulation while still occasionally referencing Parameters.day (something I don’t need to change).
One of the problems is that I don’t have a good sense of class privileges in Java.
Suggestions appreciated.
If you make all of them non-final, then you can just set them directly from your command-line arguments before instantiating the
Simulationclass: