面试题:What will happen when you attempt to compile and run the following code
2010-03-11 10:50
2481 查看
在论坛看到一个面试题,拿来和大家分享
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.
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.
相关文章推荐
- You receive a C4226 or a C4236 error message when you compile code to contain the keyword in 32-bit Visual C++(ZZ)
- use code to generate Silverlight Controls at run time, and serialize the control into XAML file
- The different way to start a service(Bind_auto_create will just create a service and the service will exit when unbind)
- When starting a Java debug session with Team Debug enabled, you may receive the following error and
- What is the difference between ./ and sh to run a script?
- rror Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences -> SQL Editor and reconnec
- What Can Happen When You Try to Convert a Standard SharePoint 2010 Farm to Enterprise
- How to resolve mysql problem when you get code 2003(10061) and 1130
- The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact.
- HikariCP 脑火Failed to obtain JDBC Connection: You need to run the CLI build and you need target/classes in your classpath to run.
- It is said that wars in the 21st century will be fought over water. Do you agree? What do you think can be done now to alleviate
- c# - Start to compile and run mono c# code
- The following code fails when ExecMethod is called. Can anyone pinpoint what I am doing wrong?
- What To Do When The Boss Begs You To Stay
- If you want to paste code into IPython, try the %paste and %cpaste magic functions.
- What happens when you type an URL in the browser and press enter?
- 每日刷题(2015/6/23)You are given the source to an application which crashes when it is run.
- What is the difference between SET and SELECT when assigning values to variables, in T-SQL?
- Which of the following expression(s) will NOT cause an error when used to replace the ***** in the C
- Attempt to load Oracle client libraries threw BadImageFormatException. This problem will occur when running in 64 bit mode with the 32 bit Oracle client components installed.