Global variables in Java

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

How do you define Global variables in Java ?

相关标签:
24条回答
  • 2020-11-22 12:28
    public class GlobalImpl {   
    
     public static int global = 5;
    
    }
    

    you can call anywhere you want:

    GlobalImpl.global // 5
    
    0 讨论(0)
  • 2020-11-22 12:28

    In general, Java doesn't have any global variables. Other than local variables, all variables comes under the scope of any class defined in the program. We can have static variables to have the scope of global variables.

    0 讨论(0)
  • 2020-11-22 12:29
    // Get the access of global while retaining priveleges.
    // You can access variables in one class from another, with provisions.
    // The primitive must be protected or no modifier (seen in example).
    
    // the first class
    public class farm{
    
      int eggs; // an integer to be set by constructor
      fox afox; // declaration of a fox object
    
      // the constructor inits
      farm(){
        eggs = 4;
        afox = new fox(); // an instance of a fox object
    
        // show count of eggs before the fox arrives
        System.out.println("Count of eggs before: " + eggs);
    
        // call class fox, afox method, pass myFarm as a reference
        afox.stealEgg(this);
    
        // show the farm class, myFarm, primitive value
        System.out.println("Count of eggs after : " + eggs);
    
      } // end constructor
    
      public static void main(String[] args){
    
        // instance of a farm class object
        farm myFarm = new farm();
    
      }; // end main
    
    } // end class
    
    // the second class
    public class fox{
    
      // theFarm is the myFarm object instance
      // any public, protected, or "no modifier" variable is accessible
      void stealEgg(farm theFarm){ --theFarm.eggs; }
    
    } // end class
    
    0 讨论(0)
  • 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!

    0 讨论(0)
  • 2020-11-22 12:33

    You are better off using dependency injection:

    public class Globals {
        public int a;
        public int b;
    }
    
    public class UsesGlobals {
        private final Globals globals;
        public UsesGlobals(Globals globals) {
            this.globals = globals;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 12:33

    without static this is possible too:

    class Main {
      String globalVar = "Global Value";
    
      class Class1 {
        Class1() {
          System.out.println("Class1: "+globalVar);
          globalVar += " - changed";
      } }
      class Class2 {
        Class2() {
          System.out.println("Class2: "+globalVar);
      } }
    
      public static void main(String[] args) {  
        Main m = new Main();
        m.mainCode();
      }
      void mainCode() {
        Class1 o1 = new Class1();
        Class2 o2 = new Class2();
      }
    }
    
    /*
    Output:
    Class1: Global Value
    Class2: Global Value - changed
    */
    
    0 讨论(0)
提交回复
热议问题