Above program not from any SCJP book.
after running above program, I am getting output as
list length ----1
list length ----2
I am not getting, why program not throwing NPE? Could somebody explain me?
here the trick is to see the scopes of different block variables.
main() ----- list ---> ArrayList()
after first call to modifyObject (list);
modifyObject(ArrayList listObj) -- varible listObj ----> arrayList object with value {1} // stackTrace
main() -------------- varible list ------------------------>
so both varbles list and listObj refers to same obj with value 1.
now the local variable listObj=null, but the list local varbiable in main method still refers to obj with value 1.
now, after second call from finally clause
again
modifyObject(ArrayList listObj) -- varible listObj ----> arrayList object with value {1,1} // stackTrace
main() -------------- varible list ------------------------>
so now the list size comes 2, not the NPE.
Hope this is clear now.