[语法] 区域变量跟全域变量都有相同变量情况

楼主: shehrevar (阿浩)   2016-10-29 12:30:40
public class Test {
static int x=1;
static Test ref;
public static void main(String[] args) {
int x=0;
System.out.println(ref);
System.out.println(x);
}
}
请教一下,现在的X输出会是0,因为方法内有X为0的变量
但是如果把他拿掉之后,输出会找到X为1的变量
能否解释一下逻辑,以及先后顺序的概念
奉上100p,谢谢
作者: kyleJ (资工人)   2016-10-29 12:45:00
两者同名的话先找区域变量同名的时候你可以使用Test.x来指定是全域变量那个全域变量叫class field类别变量 其实也不是真的全域啦以你现在的code来看:使用 x 会输出0使用 Test.x 会输出1
楼主: shehrevar (阿浩)   2016-10-29 13:56:00
感谢~~P币晚点奉上
作者: pttworld (批踢踢世界)   2016-10-29 14:06:00
语言定义,scope。

Links booklink

Contact Us: admin [ a t ] ucptt.com