您的位置:首页 > 编程语言 > Java开发

JAVA中关于主方法调用非静态方法的问题

2015-10-07 00:34 567 查看
1、已知代码如下:

</pre><pre name="code" class="java"><span style="font-size:14px;">public class Test{
long a[]=new long[10];
public static viod main(String arg[]){
System.out.print(a[6]);
}
}</span>
请问哪个语句是正确的( )。

A.输出为null

B.输出为0

C.编译时出错

D.运行时出错

答:C

因为a的修饰符不是static,所以在main( )方法执行System.out.print(a[6])时,数组a[]还没有被初始化,也就导致了编译出错。

把a的修饰符改为静态,打印的结果就是0,也可以把 long a[]=newlong[10] 写在main方法里就不需要静态修饰符。

public class Person

{

static int arr[]=new int[10];

public static void main(String a[])

{

System.out.println(arr[1]);

}

}

正确的是?

A 编译时将产生错误;

B 编译时正确,运行时将产生错误;

C 输出零;

D 输出空。

答:C



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: