Global variables in Java

2018-12-31 16:34发布

How do you define Global variables in Java ?

标签: java
19条回答
倾城一夜雪
2楼-- · 2018-12-31 17:03

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.

查看更多
骚的不知所云
3楼-- · 2018-12-31 17:03

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

查看更多
梦该遗忘
4楼-- · 2018-12-31 17:06

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;
查看更多
流年柔荑漫光年
5楼-- · 2018-12-31 17:06

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.

查看更多
裙下三千臣
6楼-- · 2018-12-31 17:07

There is no global variable in Java

Nevertheless, what we do have is a static keyword and that is all we need. Nothing exists outside of class in Java. The static keyword represents a class variable that, contrary to instance variable, only has one copy and that transcends across all the instances of that class created, which means that its value can be changed and accessed across all instances at any point.

If you need a global variable which can be accessed beyond scopes, then this is the variable that you need, but its scope exists only where the class is, and that will be all.

查看更多
高级女魔头
7楼-- · 2018-12-31 17:08

To define Global Variable you can make use of static Keyword

public final class Tools {
  public static int a;
  public static int b;
}

now you can access a and b from anywhere by calling

Tools.a;
Tools.b;

Yoy are right...specially in J2ME... You can avoid NullPointerException by putting inside your MidLet constructor (proggy initialization) this line of code:

new Tools();

This ensures that Tools will be allocated before any instruction that uses it.

That's it!

查看更多
登录 后发表回答