Global variables in Java

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

How do you define Global variables in Java ?

相关标签:
24条回答
  • 2020-11-22 12:19

    Going by the concept, global variables, also known as instance variable are the class level variables,i.e., they are defined inside a class but outside methods. In order to make them available completely and use them directly provide the static keyword. So if i am writing a program for simple arithmetical operation and it requires a number pair then two instance variables are defined as such:

    public class Add {
        static int a;
        static int b; 
        static int c;
    
        public static void main(String arg[]) {
            c=sum();
            System.out.println("Sum is: "+c); 
        }
    
        static int sum() {
           a=20;
           b=30;
           return a+b;   
        }
    }
    
    Output: Sum is: 50
    

    Moreover using static keyword prior to the instance variables enable us not to specify datatypes for same variables again and again. Just write the variable directly.

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

    Lots of good answers, but I want to give this example as it's considered the more proper way to access variables of a class by another class: using getters and setters.

    The reason why you use getters and setters this way instead of just making the variable public is as follows. Lets say your var is going to be a global parameter that you NEVER want someone to change during the execution of your program (in the case when you are developing code with a team), something like maybe the URL for a website. In theory this could change and may be used many times in your program, so you want to use a global var to be able to update it all at once. But you do not want someone else to go in and change this var (possibly without realizing how important it is). In that case you simply do not include a setter method, and only include the getter method.

    public class Global{
        private static int var = 5;
    
        public static int getVar(){
            return Global.var;
        }
    
        //If you do not want to change the var ever then do not include this
        public static void setVar(int var){
            Global.var = var;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 12:24

    To define Global Variable you can make use of static Keyword

    public class Example {
        public static int a;
        public static int b;
    }
    

    now you can access a and b from anywhere by calling

    Example.a;
    
    Example.b;
    
    0 讨论(0)
  • 2020-11-22 12:24

    You don't. That's by design. You shouldn't do it even if you could.

    That being said you could create a set of public static members in a class named Globals.

    public class Globals {
       public static int globalInt = 0;
       ///
    }
    

    but you really shouldn't :). Seriously .. don't do it.

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

    There is no such thing as a truly global variable in Java. Every static variable must belong to some class (like System.out), but when you have decided which class it will go in, you can refer to it from everywhere loaded by the same classloader.

    Note that static variables should always be protected when updating to avoid race conditions.

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

    Creating an independent file, eg. Example.java to use the 1st solution, is just fine. You can do that also within the app, if e.g. the global variables are special to your current app, etc.:

    Create a class at the beginning and declare your variables in there:

    class Globals {
      static int month_number;
      static String month_name;
    }
    

    You can then access these variables -- using them as 'Globals.month_number', etc. -- from averywhere in your app.

    0 讨论(0)
提交回复
热议问题