SCJP中的一题(关于回收)

来源:互联网 发布:aes密码本算法 编辑:程序博客网 时间:2024/04/30 08:46

class TestA {
 TestB b;
 TestA() {
 b = new TestB(this);
 }
 }
class TestB {
 TestA a;
 TestB(TestA a) {
  this.a = a;
  }
  }
class TestAll {
  public static void main (String args[]) {
  new TestAll().makeThings();
  // ...code continues on
  }
  void makeThings() {
  TestA test = new TestA();
  }
  }
 
Which two statements are true after line 15, before main completes? (Choose two)
A. Line 15 causes a stack overflow.
B. An exception is thrown at runtime.
C. The object referenced by a is eligible for garbage collection.
D. The object referenced by b is eligible for garbage collection.
E. The object referenced by a is not eligible for garbage collection.
F. The object referenced by b is not eligible for garbage collection.

答案是c,d。它是这样解释的:This is a typical example of the island of isolation. On line 15, the two objects TestA and TestB have a eference to one an other. Therefore, the correct answers are C. and D. A key point to remember is that an object that is referenced by another object can be eligible for
garbage collection if the two objects form an island of isolated objects.