public class Test1 {
static int i=getValue();
static int j=10;
public static void main(String[] args) {
System.out.println(i);
}
private static int getValue() {
return j;
}}
输出结果为 0;我不知道是为什么,请高手赐教!!谢谢!!
static int i=getValue();
static int j=10;
public static void main(String[] args) {
System.out.println(i);
}
private static int getValue() {
return j;
}}
输出结果为 0;我不知道是为什么,请高手赐教!!谢谢!!
public class Test1 {
static int j=10;
static int i=getValue();public static void main(String[] args) {
System.out.println(i);
}
private static int getValue() {
return j;
}}
你改成这样再看看输出是什么??
static int i=getValue();
static int j=10;同时静态初始化, 那就就有先后顺序。 初始化i的时候 你的 j并没有定义。
public class Tes
{
static int j=10;
static int i=getValue();
public static void main(String[] args) {
System.out.println(i);
}
private static int getValue() {
System.out.println(j);
return j;
}
}请看上面代码 你可以交换初始化i ,j位置来看打印的j的值 你就明白了
当运行static int i=getValue();这一句时,j这个变量还只是一个空的内存,没有被赋值了,由于i是int类型的,所以默认是0,
若果这样写:
static int j=10;
static int i=getValue();
这样就会输出10了。