SCJP 1.6版考題 039

出自 陳富國維基館
於 2013年3月30日 (六) 07:54 由 Ikk (對話 | 貢獻) 所做的修訂 (新頁面: 11. public class ItemTest{ 12. private final int id; 13. public ItemTest(int id){this.id = id;} 14. public void updateId(int newId){id = newId;} 15. 16. public static voi...)
(差異) ←上個修訂 | 最新修訂 (差異) | 下個修訂→ (差異)
前往: 導覽搜尋
11. public class ItemTest{
12.   private final int id;
13.   public ItemTest(int id){this.id = id;}
14.   public void updateId(int newId){id = newId;}
15.
16.   public static void main(String[] args){
17.     ItemTest fa = new ItemTest(42);
18.     fa.updateId(69);
19.     System.out.println(fa.id);
20.   }
21. }

Which one statments are true? (Choose one.)
  A. Compilation fails.
  B. An exception is thrown at runtime.
  C. The attribute id in the ItemTest object remains unchanged.
  D. The attribute id in the ItemTest object is modified to the new value.
  E. A new ItemTest object is created with the preferred value in the id attribute.


解答


Ans: A

解說: id是常數(final),updateId要變更id是一個錯誤的動作