关于变量初始化的问题!
Q:
What would be the result of attempting to compile and run the
following piece of code?
public class Test {
public int x;//注意哦,就是这一行,还有下面有注释的一行
public static void main(String args[]){
System.out.println("Value is " + x);
}
}
A. The output "Value is 0" is printed.
B. Non-static variable x cannot be referenced from a static context..
C. An "illegal array declaration syntax" compiler error occurs.
D. A "possible reference before assignment" compiler error occurs.
E. An object of type ArrayIndexOutOfBoundsException is thrown.
答案:B.
Q:
What would be the result of attempting to compile and run the
following piece of code?
public class Test {
public static void main(String args[]){
int x;//注意哦,就是这一行,还有上面有注释的一行
System.out.println("Value is " + x);
}
}
A. The output "Value is 0" is printed.
B. An object of type NullPointerException is thrown.
C. An "illegal array declaration syntax" compiler error occurs.
D. A "possible reference before assignment" compiler error occurs.
E. An object of type ArrayIndexOutOfBoundsException is thrown.
答案:D.
java中间有这样的定义吗?
即方法外面的变量会自动赋默认值,而方法里面的
变量必须初始化才能使用!
问题点数:10、回复次数:6Top
1 楼ChDw(米)回复于 2001-12-06 17:14:07 得分 4
是的,的确有这样的定义的Top
2 楼Patrick_DK(我有我的调调,就是这么屌)回复于 2001-12-06 17:16:40 得分 2
楼上,第一个问题很清楚,对非Static的类成员必须用类的实例来调用
第二个问题你是不是抄错了,连初始化都没有,怎么调用变量x啊?Top
3 楼Patrick_DK(我有我的调调,就是这么屌)回复于 2001-12-06 17:20:53 得分 2
方法外面的变量是类的成员,即属性,会自动分配一个默认值(但不能使用),必须初始化以后才能访问.
方法内部的变量是局部变量Top
4 楼Patrick_DK(我有我的调调,就是这么屌)回复于 2001-12-06 21:14:21 得分 2
刚才试了一下,class level的变量自动分配的默认值是可以直接使用的.
操,我看的书太旧了,SorryTop
5 楼yuking(郭靖)回复于 2001-12-06 21:24:40 得分 0
太长了,我没时间看!Top
6 楼bookbobby(书呆)回复于 2001-12-07 12:22:28 得分 0
谢谢各位指点Top




