Basically, I have a boolean which is used by like 20% of all my classes on a large API project. Everything, from utility methods to larger classes, uses it. I can set the state (which will not change) on the program start, but don't know the "best" way to access it.
Initially, I went for dependency injection. Quickly, method parameter count grew as almost everything needed it. It was a pain watching the single boolean in almost every parameter in certain classes. I decided to try out a static method. I came up with the following class structure:
public final class MyClass {
private static boolean neededBoolean;
public MyClass(boolean bool) {
MyClass.setBoolean(bool);
}
private static void setGems(boolean bool) {
MyClass.neededBoolean = bool;
}
public static boolean getNeededBoolean() {
return neededBoolean;
}
}
This felt strange though, most static methods usually don't read internal variables, and any program could easily (purposefully or not) change the variable for everything. I get I could also use a singleton, but I've been told those are best for loggers and other uses, not reading static variables. I also don't want to make the boolean public so any other classes could edit it.
So what's the best choice here? Overusing dependency injection, a singleton, or my current system