public class Exam5 {
static int s; //成员变量,类变量
int i; //成员变量,实例变量
int j;
{
int i = 1;
i ++; //就近原则,this.i可使用类变量
j ++;
s ++;
}
public void test(int j) { //形参,局部变量j
j ++; //就近原则,不影响成员变量j
i ++;
s ++;
}
public static void main(String[] args) { //形参,局部变量args
Exam5 obj1 = new Exam5(); //clinit,init,s = 1,i = 0,j = 1
Exam5 obj2 = new Exam5(); //clinit,init,s = 2,i = 0,j = 1
obj1.test(10); //s = 3,i = 1,j = 1
obj1.test(20); //s = 4,i = 2,j = 1
obj2.test(30); //s = 5,i = 1,j = 1
System.out.println(obj1.s + "," +obj1.i + "," +obj1.j);
System.out.println(obj2.s + "," +obj2.i + "," +obj2.j);
}
}
执行结果:
5,2,1
5,1,1
传送门:https://www.bilibili.com/video/BV1nJ411M7ZJ?p=6