多选题 Given the SampleClass, what is the value of currentCount for the instance of object x after the code segment had be executed?
SampleClass x = new SampleClass();
SampleClass y = new SampleClass();
x.increaseCount();
public class SampleClass {
private static int currentCount=();
public SampleClass() {
currentCount++;
}
public void increaseCount() {
currentCount++;
}
}
  • A. 0
  • B. 1
  • C. 2
  • D. 3
  • E. Compiler error
  • F. Runtime error
【正确答案】 D
【答案解析】变量currentCount是一个静态变量。因此,该类的每个实例访问的都是同一个变量。代码段创建该类的两个新实例。构造函数每次递增此变量。最后调用increaseCount方法,它也递增该变量。A、B、C、E和F不正确。A、B和C不正确,因为currentCount是一个静态变量,每创建一个对象以及进入increaseCount方法,都会递增同一个值。E和F不正确,因为这是正确的代码。