static

来源:互联网 发布:a1530支持什么网络 编辑:程序博客网 时间:2024/06/06 00:20
能够独立于类的所有对象进行使用的成员
可以在创建类的任何对象之前访问该成员
本质上是全局变量
声明为静态的方法有几个限制:
  1. 它们只能直接调用其他静态方法
  2. 它们只能直接访问静态数据
  3. 它们不能以任何方式引用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);
  }
}
0 0