J002.关于static变量
//:StaticTest.java
class StaticClass
{
static int i = 1;
int j = 2;
}
class StaticTest
{
public static void main(String[] args)
{
StaticClass st1 = new StaticClass();
StaticClass st2 = new StaticClass();
st1.i ++ ;
st1.j ++ ;
System.out.println("st1.i = " + st1.i + " st1.j = " + st1.j);
System.out.println("st2.i = " + st2.i + " st2.j = " + st2.j);
}
}
/*
Output:
st1.i = 2 st1.j = 3
st2.i = 2 st2.j = 2
*///:~
所以,static变量只占用一个存储空间,即为“class data”,”meaning that the data and method exist only for the class as a whole,and not for any particular objects of class”(Thinking in Java)