JAVA每日随手练【类和对象】
1.单选题一
阅读如下代码。 请问,对语句行 test.hello(). 描述正确的有()
A.能编译通过,并正确运行
B.因为使用了未初始化的变量,所以不能编译通过
C.以错误的方式访问了静态方法
D.能编译通过,但因变量为null,不能正常运行
class Test {
public static void hello() {
System.out.println("hello");
}
}
public class MyApplication {
public static void main(String[] args) {
// TODO Auto-generated method stub
Test test=null;
test.hello();
}
}
正确答案:A
【分析】:Test中的hello方法是静态方法,静态方法的调用与对象是否实例化无关。直接使用Test.hello()也可以编译通过并成功运行
2.单选题二
下面代码的运行结果