SCJP題庫第133題

Given:
10. public class ClassA{
11. public void methodA(){
12.   ClassB classB = new ClassB();
13.   classB.getValue();
14. }
15. }
And:
20. class ClassB{
21.   public ClassC classC;
22.
23.   public String getValue(){
24.   return classC.getValue();
25.   }
26. }


And:
30. class ClassC{
31. public String value;
32.
33. public String getValue(){
34.   value = "ClassB";
35.   return value;
36. }
37. }
And given:
ClassA a = new ClassA();
a.methodA();
What is the result?
 
 
A. Compilation fails.
B. ClassC is displayed.
C. The code runs with no output.
D. An exception is thrown at runtime.
 
Ans: D
 
解說:
classC.getValue() 要叫用類別C中的物件方法getValueclassC現值為null,未指向一個類別C的物件,以null參考來叫用一個物件方法會得到一個nullpointException例外。

Comments

Popular posts from this blog

Android+Google Map API v3 Geocoding(地址轉經緯度度