TestStatic
public class TestStatic {
static int a;
int b;
static int c;
public int aMethod(){
a++;
return a;
}
public int bMethod(){
b++;
return b;
}
public int cMethod(){
c++;
return c;
}
public static void main(String[] args) {
TestStatic test1 = new TestStatic();
TestStatic test2 = new TestStatic();
TestStatic test3 = new TestStatic();
test1.aMethod();
System.out.println(test1.aMethod());
test2.bMethod();
System.out.println(test2.bMethod());
test3.cMethod();
System.out.println(test3.cMethod());
}
}
结果输出 :
2
2
2
解析:
没有赋值,默认初始化值都为0
调用两次方法,进行两次++运算
所以结果都为2