SCJP(OCPJP) 考古題解析 第39題

Given:
public class ItemTest {
    private final int id;
    public ItemTest(int id) {this.id = id;}
    public void updateId(int newId) {id = newId;}

    public static void main(String[] args) {
        ItemTest fa = new ItemTest(42);
        fa.updateId(69);
        System.out.println(fa.id);
    }
}

Which four statments is true?


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.

答案:


解析:

如果class內建立了一個未指派的final常數

每個建構子都必須要幫它初始

且不得在方法內做指派



0 意見:

張貼留言