首页 > 代码库 > java引用被设置为null的疑惑

java引用被设置为null的疑惑

a=null;

 1 public class C { 2       3     protected A webDigester = new A(" first one "); 4      5     public void test(){ 6      7         A a; 8          9         a=webDigester;10         11         a.add(" second ");        12         13         a=null;14         15         webDigester.display();16         //猜猜会打印出什么呢17     }18     public static void main(String[] args) {19 20         C c=new C();21         22         c.test();23     }24 25 }

 

 1 import java.util.ArrayList; 2  3  4 public class A { 5      6         public ArrayList<Object> al=new ArrayList<Object>(); 7  8         public void display() 9         {10             System.out.println("-----start-------");11             for(Object o:al)12             {13                 System.out.println(" ["+o.toString()+"] ");14             }15             System.out.println("-----end-------");16         }17         18         public A(String msg){19             al.add(msg);20         }21         22         public void add(String msg){23             al.add(msg);24         }25         26         public void  distory()27         {28             al.clear();29         }30         31 }

 

java引用被设置为null的疑惑