问题
单项选择题
阅读下列代码public class Test{ String s="One World One Dream"; public static void main(String args[]){System. out. println(s); }} 其运行结果是
A.args
B.One World One Dream
C.s
D.编译时出错
答案
参考答案:D
解析: 字符串s没有被声明成静态的,题中当主函数调用打印函数输出s的内容时,Test类还没有被实例化,也就没有字符串常量s了。可将字符串s声明为static类型的,或者在打印字符前先实例化Test类,再打印这个对象中的字符串。