关于变量初始化的问题!

bookbobby 2001-12-06 05:08:39
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中间有这样的定义吗?
即方法外面的变量会自动赋默认值,而方法里面的
变量必须初始化才能使用!
...全文
87 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
bookbobby 2001-12-07
  • 打赏
  • 举报
回复

谢谢各位指点
yuking 2001-12-06
  • 打赏
  • 举报
回复
太长了,我没时间看!
Patrick_DK 2001-12-06
  • 打赏
  • 举报
回复
刚才试了一下,class level的变量自动分配的默认值是可以直接使用的.

操,我看的书太旧了,Sorry
Patrick_DK 2001-12-06
  • 打赏
  • 举报
回复
方法外面的变量是类的成员,即属性,会自动分配一个默认值(但不能使用),必须初始化以后才能访问.

方法内部的变量是局部变量
Patrick_DK 2001-12-06
  • 打赏
  • 举报
回复
楼上,第一个问题很清楚,对非Static的类成员必须用类的实例来调用
第二个问题你是不是抄错了,连初始化都没有,怎么调用变量x啊?
ChDw 2001-12-06
  • 打赏
  • 举报
回复
是的,的确有这样的定义的

62,629

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧