6

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 :)

Submerged
  • 646
  • 1
  • 10
  • 16
  • "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?" If this is what you want, it's a Singleton, or a global variable or whatever name you prefer for it. – Captain Giraffe Mar 06 '12 at 20:16
  • 1
    Your first paragraph actually does _not_ make sense. You misinterpreted what you were reading, or your reference is very bad. Stuff does not "just disappear" from memory in Java. – toto2 Mar 06 '12 at 20:48
  • 1
    Your example is actually one form of the singleton pattern. – toto2 Mar 06 '12 at 20:51
  • 1
    This is good to know, do you have a source? I read on stack overflow saying that the class will eventually be unloaded and the variables will be null which scared me and made me post this in the first place. I guess you can't believe everything. Had a few upvotes which lended to the credibility, though. – Submerged Mar 06 '12 at 20:58
  • Look at the post here and the second comment with 14 up votes saying NOT to use the design pattern I posted above. http://stackoverflow.com/questions/4646577/global-variables-in-java – Submerged Mar 06 '12 at 21:00
  • 1
    You should give the reference to the "disappearing values" so I can downvote it; it is complete junk. – toto2 Mar 06 '12 at 21:06
  • Just did :) Maybe I misinterpreted it? The relevant text is "That being said you could create a set of public static members in a class named Globals. but you really shouldn't :). Seriously .. don't do it." and someone backs it up saying you will lose the variables. – Submerged Mar 06 '12 at 21:08
  • The post with the 14 upvotes says you should not use _global variables_. The singleton pattern is a way to define a global variable. You are involuntarily trolling here: there is a lot of debate about using or not using global variables. I am on the side that you should not use them. If some object needs some value, it should be passed as a parameter, not obtained from a global variable. Code with global variables becomes unreadable and unmaintainable unless it's quite shorth. – toto2 Mar 06 '12 at 21:11
  • I understand that since your variable is just some debugging flag and that you don't want to pass it everywhere to every method. You should probably just try to use a debugger and also test your program bits by bits as you progress so that you don't need to build some debugging infrastructure within your code. – toto2 Mar 06 '12 at 21:15
  • Just to clarify: I upvoted the [answer](http://stackoverflow.com/questions/4646577/global-variables-in-java#4646605) in the other post saying that you should not use global variables. It's the second comment to that answer that makes no sense (losing static values). – toto2 Mar 06 '12 at 21:21
  • Didn't mean to troll; I just wanted to make sure it was OK to use Global Variables from a reliability point of view. I don't care so much about semantics and I understand the pros and cons to using them and not, as long as the cons doesn't include "it will turn null eventually". Much appreciated and thanks for all your comments! – Submerged Mar 06 '12 at 22:32

2 Answers2

4

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:

public enum Constants {
    INSTANCE;

    public void isInDebugMode() { 
        return true;
    }
}

Here is how you use it (even from static code):

if(Constants.INSTANCE.isInDebugMode()) {....}

You might also think about some more sophisticated solution:

public enum Constants {
    DEBUG(true),
    PRINT_VARS(false);

    private boolean enabled;

    private Constants(boolean enabled) {
        this.enabled = enabled;
    }

    public boolean isEnabled() {
        return enabled;
    }
}

Example usage:

if(Constants.DEBUG.isEnabled()) {....}
Sebastian Łaskawiec
  • 2,667
  • 15
  • 33
0

It is best practice to use a static method, instead of a variable:

public class MasterParameters {

public static boolean DebugMode(){
     return true; // change this
}
protected MasterParameters(){
    // Exists only to defeat instantiation.
}
arc
  • 584
  • 2
  • 5