Sorry for my poor english, I test the List object with clear method and to assign null value
here is code
public class TestList {
private List<String> list;
private List<List<String>> mainList;
public TestList(){
list = new ArrayList<String>();
mainList = new ArrayList<List<String>>();
}
public static void main(String arg[]){
String[] mylist = {"sunday","monda","tuesday","wednesday","thurday","friday","saturday"};
TestList tl1 = new TestList();
for(String s : mylist)
tl1.list.add(s);
tl1.mainList.add(tl1.list);
//tl1.list.clear(); // here if clear then not getting the list
tl1.list = null; ///// but here to assign null still getting list
System.out.println("after clear");
System.out.println();
for(List<String> list : tl1.mainList){
for(String s : list){
System.out.println(s);
}
}
}
}
so my question is when I clear the tl1.list using tl1.list.clear(); and then print the value it’s clear but when I assing null to this tl1.list then it will print the value how?
tl1.list.clear()clears the listlistwhich was added tomainListas well. As Java is object-orientedlistandmainList.get(0)reference exactly the same object. If you settl1.listto null, you only dereference the object, it is still available inmainList.I’m not sure if I understood your question, but I guess this is what you asked…