Global variables in Java

后端 未结 24 1857
青春惊慌失措
青春惊慌失措 2020-11-22 11:56

How do you define Global variables in Java ?

24条回答
  •  隐瞒了意图╮
    2020-11-22 12:30

    Nothing should be global, except for constants.

    public class MyMainClass {
        public final static boolean DEBUGMODE=true;
    }
    

    Put this within your main class. In other .java files, use it through:

    if(MyMainClass.DEBUGMODE) System.out.println("Some debugging info");
    

    Make sure when you move your code off the cutting room floor and into release you remove or comment out this functionality.

    If you have a workhorse method, like a randomizer, I suggest creating a "Toolbox" package! All coders should have one, then whenever you want to use it in a .java, just import it!

提交回复
热议问题