您的位置:首页 > 移动开发

面试题:What will happen when you attempt to compile and run the following code

2010-03-11 10:50 2481 查看
在论坛看到一个面试题,拿来和大家分享

What will happen when you attempt to compile and run the following code
class MyClass{
static String myName="Webs-TV";

MyClass getMyClass(){
System.out.println(myName);
return null;
}
public static void main(String[] args){
System.out.println(new MyClass().getMyClass().myName);
}
}
  
A.Compiler time error
B.runtime error
C.Print "Webs-TV" twice
D.Print "Webs-TV" once

 

 

----------

答案选C

 

主要考察对静态变量的认识

 

----------------

转帖子内的一回复

 

static 声明的静态属性在不用实例化对象的情况下也能调用。题目中的new MyClass().getMyClass().myName可以解读为匿名对象了MyClass()调用getMyClass()方法返回null相当于MyClass a=null;在调用a的静态属性a.myName。所以答案选C.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  面试 null string class c
相关文章推荐