SCJP 1.6版考題 035

出自 陳富國維基館
前往: 導覽搜尋
10. public class Hello{
11.   String title;
12.   int value;
13.   public Hello(){
14.     title += " World";
15.   }
16.   public Hello(int value){
17.     this.value = value;
18.     title = "Hello";
19.     Hello();
20.   }
21. }
and:
30. Hello c = new Hello(5);
31. System.out.print(c.title);


What is the result?
  A. Hello
  B. Hello World
  C. Compilation fails.
  D. Hello World 5
  E. The code runs with no output.
  F. An exception is thrown at runtime.


解答


Ans: C

解說:

對本身建構子的呼叫寫法為this(), 另外,
若有本身建構子的呼叫或對父類別建構子的呼叫,這二種呼叫必須出現在首行