I’ve been reading that using static variables in a class that’s never instantiated is a bad idea, because the variables may turn null when the class is not longer in memory. Makes sense.
This is what I’ve been doing for an example
public class MasterParameters {
public static boolean DEBUG_MODE = true;
protected MasterParameters(){
// Exists only to defeat instantiation.
}
}
I’ve also heard using a Singleton is equally bad and people suggest using “dependency injection” — This seems complicated and overkill for what I need, however. Am I just not looking at the right examples?
I want an easy way to define a variable in one spot that can be accessed from anywhere in my code without having to pass a parameters object around. What do you suggest?
Thanks 🙂
I would suggest Singleton pattern (I know many people don’t like it), but it seems the simplest solution that will work. Take a look at this piece of code:
Here is how you use it (even from static code):
You might also think about some more sophisticated solution:
Example usage: