Java里没有全局变量的概念吧?只有静态变量和静态方法:class A {
static int i = 100; //i在整个类中有效,但不知能不能算“全局变量”
static int hello() {}
}调用的时候分别用
A.i
A.hello()
static int i = 100; //i在整个类中有效,但不知能不能算“全局变量”
static int hello() {}
}调用的时候分别用
A.i
A.hello()
public static String name = "jack";//类静态字段
public static ClassA ca = new ClassA();//类静态字段(对象)
//类静态方法
public static int hello() {
return 1;
}
}调用时候直接
Const.name;
Const.ca.;
Const.hello();
就可以了.
Class GlobalVar {
public static global_var;
} 在类GlobalVar中定义变量global_var为public static,使得其它类可以访问和修改该变量。