java源代码 麻烦帮我找个问题

来源:百度知道 编辑:UC知道 时间:2024/07/04 18:17:53
public class Something {
public static void main(String[] args) {
Other o = new Other();
new Something().addOne(o);
}
public void addOne(final Other o) {
o.i++;
}
}
class Other {
public int i;
}

大家看这段代码哪里错了,并告诉我。在JAVA编译不会报错的。这是一道题目,是排错的。

这段代码确实是没有错误. int的如果没有初始化,默认值为0
class Something {
public static void main(String[] args) {
Other o = new Other();
new Something().addOne(o);
}

public void addOne(final Other o) {
System.out.println(o.i);
o.i++;
System.out.println(o.i);
o.i++;
System.out.println(o.i);
o.i++;
System.out.println(o.i);
}
}

class Other {
public int i;
}
下面是结果
0
1
2
3
MyEclipse 6.01 下面调试的

你在Other里面的i没有初值,所以你addOne里面的o.i++会报错

语法没错误,也不会产生异常!

这个没有错,怎么找错嘛。