class Snoochy {                                       
 Boochy booch;                                         
 public Snoochy() { booch = new Boochy(this); }        
 }                                                     
                                                       
 class Boochy {                                        
 Snoochy snooch;                                       
 public Boochy(Snoochy s) { snooch = s; }              
 }                                                     
                                                       
 the statements:                                       
                                                       
 public static void main(String[] args) {              
 Snoochy snoog = new Snoochy();                        
 snoog = null;                                         
 // more code here                                     
 }   A.  None of these objects are eligible for garbage collection.                                     
B.  Only the object referenced by booch is eligible for garbage collection.                        
C.  Only the object referenced by snoog is eligible for garbage collection.                        
D.  Only the object referenced by snooch is eligible for garbage collection.                       
E.  The objects referenced by snooch and booch are eligible for garbage collection.                
                                                  
选啥,为什么?

解决方案 »

  1.   

    选E 
    这两个对象, 如果没有第三方引用到其中之一, 也就是外界无法访问到这两对象 
    只有互相引用的话,这两个对象会连在一起成为垃圾,可以被回收 测试程序: public class Test
    {
        public static void main(String[] args) 
        {              
            Snoochy snoog = new Snoochy();                        
            snoog = null;
            while(true)
                System.gc();
        }
    }class Boochy 
    {                                        
        Snoochy snooch; 
        public Boochy(Snoochy s) 
        { 
            snooch = s;
        }          
        
        public void finalize()
        {
            System.out.println("Boochy garbage collected.");
        }
    }class Snoochy 
    {                                      
        Boochy booch;                                        
        public Snoochy() 
        { 
            booch = new Boochy(this); 
        } 
        
        public void finalize()
        {
            System.out.println("Snoochy garbage collected.");
        }
    }程序输出: 
    Boochy garbage collected. 
    Snoochy garbage collected.
      

  2.   

    等等,这句话有点漏洞,如果一个类中指向自己的一个静态实例,是回收不了的吧,single pattern