来看看这个JAVA程序

来源:百度知道 编辑:UC知道 时间:2024/06/10 00:06:43
标记的x=1,y=0;
为什么不是x=1,y=1;
帮忙看下,给个解释啊!为什么?
public class Test {
static Test st = new Test();
static int x;
static int y = 0;
public Test() {
x++;
y++;
}
public Test(int x, int y) {
this.x = x;
this.y = y;
}
public static Test getInstance() {
return st;
}
static {
int i = 0;
System.out.println("i=" + i);
}
void test() {
Inner inner2 = new Inner2();
inner2.display();
}
郁闷,为什么只能添加一张图片啊!!
这是某公司面试的笔试题

这是有关程序执行顺序的一个问题。在构造函数Test运行时,静态域的值x和y都是0,0。

把下面的代码补充执行以下,你会明白吧。

public Test() {
System.out.println("before x= "+ x +" y= "+y );
x++;
y++;
System.out.println("after x= "+ x +" y= "+y );
}

这段代码运行后才执行的静态域的赋值。

再不明白,给我发信息吧

兄弟,你连调用的类都不给出来,叫我怎么知道他返回什么啊...