• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

why object was not setting null

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sreenivasa, welcome to javaranch.

Read this campfire story if it helps ...
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also this...

http://faq.javaranch.com/java/CallByReferenceVsCallByValue

Henry
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sreenivasa,

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.
 
Sreenivasa Rao Venepalli
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Ankit for welcoming me. Thank you Milan for your response.
reply
    Bookmark Topic Watch Topic
  • New Topic