Global variables in Java

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

How do you define Global variables in Java ?

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

    very simple:

    class UseOfGlobal
    {
    private static int a;
    private static int b;
    }
    

    but it is always good to have local variables defined inside method blocks where ever possible.

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

    Understanding the problem

    I consider the qualification of global variable as a variable that could be accessed and changed anywhere in the code without caring about static/instance call or passing any reference from one class to another.

    Usually if you have class A

    public class A {
        private int myVar;
    
        public A(int myVar) {
            this.myVar = myVar;
        }
    
        public int getMyVar() {
            return myVar;
        }
    
        public void setMyVar(int mewVar) {
            this.myVar = newVar;
        }
    }
    

    and want to access and update myvar in a class B,

    public class B{
    
        private A a;
    
        public void passA(A a){
            this.a = a;
        }
    
        public void changeMyVar(int newVar){
            a.setMyvar(newVar);
        }
    }
    

    you will need to have a reference of an instance of the class A and update the value in the class B like this:

    int initialValue = 2;
    int newValue = 3;
    A a = new A(initialValue);
    B b = new B();
    b.passA(a);
    b.changeMyVar(newValue);
    assertEquals(a.getMyVar(),newValue); // true
    

    Solution

    So my solution to this, (even if i'm not sure if it's a good practice), is to use a singleton:

    
    public class Globals {
        private static Globals globalsInstance = new Globals();
    
        public static Globals getInstance() {
            return globalsInstance;
        }
    
        private int myVar = 2;
    
        private Globals() {
        }
    
        public int getMyVar() {
            return myVar;
        }
    
        public void setMyVar(int myVar) {
            this.myVar = myVar;
        }
    }
    

    Now you can get the Global unique instance anywhere with:

    Globals globals = Globals.getInstance();
    // and read and write to myVar with the getter and setter like 
    int myVar = globals.getMyVar();
    global.setMyVar(3);
    
    0 讨论(0)
  • 2020-11-22 12:42

    As you probably guess from the answer there is no global variables in Java and the only thing you can do is to create a class with static members:

    public class Global {
        public static int a;
    }
    

    You can use it with Global.a elsewhere. However if you use Java 1.5 or better you can use the import static magic to make it look even more as a real global variable:

    import static test.Global.*;
    
    public class UseGlobal {
        public void foo() {
            int i = a;
        }
    }
    

    And voilà!

    Now this is far from a best practice so as you can see in the commercials: don't do this at home

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

    Object-Oriented Programming is built with the understanding that the scope of variables is closely exclusive to the class object that encapsulates those variables.

    The problem with creating "global variables" is that it's not industry standard for Java. It's not industry standard because it allows multiple classes to manipulate data asyncronized, if you're running a multi-threaded application, this gets a little more complicated and dangerous in terms of thread-safety. There are various other reasons why using global variables are ineffective, but if you want to avoid that, I suggest you resort to Aspect-Oriented Programming.

    Aspect-Oriented Programming negates this problem by putting the parent class in charge of the scope through something called "advices", which adds additional behavior to the code without actually modifying it. It offers solutions to cross-cutting concerns, or global variable usage.

    Spring is a Java framework that utilizes AOP, and while it is traditionally used for web-applications, the core application can be used universally throughout the Java framework (8.0 included). This might be a direction you want to explore more.

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

    There are no global variables in Java, but there are global classes with public fields. You can use static import feature of java 5 to make it look almost like global variables.

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

    Truly speaking there is not a concept of "GLOBAL" in a java OO program

    Nevertheless there is some truth behind your question because there will be some cases where you want to run a method at any part of the program. For example---random() method in Phrase-O-Matic app;it is a method should be callable from anywhere of a program.

    So in order to satisfy the things like Above "We need to have Global-like variables and methods"

    TO DECLARE A VARIABLE AS GLOBAL.

     1.Mark the variable as public static final While declaring.
    

    TO DECLARE A METHOD AS GLOBAL.

     1. Mark the method as public static While declaring.
    

    Because I declared global variables and method as static you can call them anywhere you wish by simply with the help of following code

    ClassName.X

    NOTE: X can be either method name or variable name as per the requirement and ClassName is the name of the class in which you declared them.

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