Given:
public class Customer {
private String fName;
private String lName;
private static int count;
public Customer(String first, String last) {
fName = first;
lName = last;
++count;
}
static {
count = 0;
}
public static int getCount() {
return count;
}
}
public class App {
public static void main(String[] args) {
Customer c1 = new Customer("Larry", "Smith");
Customer c2 = new Customer("Pedro", "Gonzales");
Customer c3 = new Customer("Penny", "Jones");
Customer c4 = new Customer("Lars", "Svenson");
c4 = null;
c3 = c2;
System.out.println(Customer.getCount());
}
}
What is the result?
A.
0
B.
2
C.
3
D.
4
E.
5
題解
先看到Customer類別的getCount類別方法,是回傳count變數的值。count變數在建構子被呼叫的時候會加1,在App類別的main方法中,一共實體化了4個Customer物件,所以最後會輸出「4」。