Given:
class Caller {
private void init() {
System.out.println("Initialized");
}
public void start() {
init();
System.out.println("Started");
}
}
public class TestCall {
public static void main(String[] args) {
Caller c = new Caller();
c.start();
c.init();
}
}
What is the result?
A.
Initialized
Started
Started
B.
Initialized
Started
Initialized
Started
Initialized
C. Compilation fails
D. An exception is thrown at runtime
題解
程式第18行使用了Caller物件的init方法。由於init方法被private修飾字修飾,它的可見度只有在Caller這個類別之內,所以TestCall類別無法去呼叫到Caller物件的init方法,會在編譯時出現錯誤。