能够独立于类的所有对象进行使用的成员
可以在
创建类的任何对象之前访问该成员
本质上是全局变量
声明为静态的方法有几个限制:
- 它们只能直接调用其他静态方法
- 它们只能直接访问静态数据
- 它们不能以任何方式引用this或super关键字
可以声明静态代码块:
// Demonstrate static variables, methods, and blocks.
class UseStatic {
static int a = 3;
static int b;
static void meth(int x) {
System.out.println("x = " + x);
System.out.println("a = " + a);
System.out.println("b = " + b);
}
static {
System.out.println("Static block initialized.");
b = a * 4;
}
public static void main(String args[]) {
meth(42);
}
}