为什么数据字段值会发生变化?我正在阅读的文字没有清楚地解释它。这一章主要是关于数据字段封装,你所做的对他们来说可能会影响代码和这类事情的小事情。
public class Questions {
private static int j = 0;
private static int i = 0;
public static void main(String[] args) {
int i = 2;
int k = 3;
{
j = 3;
System.out.println("j is " + j);
}
k = i + j;
System.out.println("k is " + k);
System.out.println("j is " + j);
}
}
/* output is:
j is 3
k is 5
j is 3
*/
//but if you do this one thing:
public class Questions {
private static int j = 0;
private static int i = 0;
public static void main(String[] args) {
int i = 2;
int k = 3;
{
int j = 3;
System.out.println("j is " + j);
}
k = i + j;
System.out.println("k is " + k);
System.out.println("j is " + j);
}
/* the output is:
j is 3
k is 2
j is 0
*/
任何输入表示赞赏!