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」。
