This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Murach's Java Programming and have Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Java Programming this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Confusion over how we reference an object added to an arraylist  RSS feed

 
Quazi Irfan
Ranch Hand
Posts: 101
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having some confusion over how java passing object to a collection. I'll explain it using the following example,



The output of the code is,


My assumptions are,
  • When we add an object to the list, we are adding the reference to the list, not the actual object. That's why the first and third line of output is pointing to same memory location.
  • When we leave the scope of main's constructor, the local reference 'a' will become invalid. So the there is no object at the memory a is pointing to.


  • So, how can we access the member field of an object through a reference that is invalid? In that location, there shouldn't be any object - but we are able to retrieve the value 10.
     
    Tim Moores
    Saloon Keeper
    Posts: 3755
    78
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    When we leave the scope of main's constructor, the local reference 'a' will become invalid. So the there is no object at the memory a is pointing to.

    No, the object lives on, because there is a reference to it in the list.
     
    Les Morgan
    Rancher
    Posts: 767
    19
    C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Tim is correct, in Java an object's life is determined by scope and reference: when all references to the object go out of scope, then the object becomes invalid and is marked for garbage collection.

    What this means is that you can create objects and assign them to a reference object that is not in the same scope, but is visible to that scope.  Since the object is referenced outside of the scope, then the object is still live when the creating scope is closed--it is held live by the reference stored in the reference object.

    Any scope that has visibility to the scope of the reference object will be able to use the object that was created.
     
    Marius Zilenas
    Greenhorn
    Posts: 5
    Linux VI Editor
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You lost reference through a, but you retain reference through a list. The JVM knows that there's a reference and your object prevails.

    Quazi Irfan wrote:I am having some confusion over how java passing object to a collection. I'll explain it using the following example,



    The output of the code is,


    My assumptions are,
  • When we add an object to the list, we are adding the reference to the list, not the actual object. That's why the first and third line of output is pointing to same memory location.
  • When we leave the scope of main's constructor, the local reference 'a' will become invalid. So the there is no object at the memory a is pointing to.


  • So, how can we access the member field of an object through a reference that is invalid? In that location, there shouldn't be any object - but we are able to retrieve the value 10.
     
    Quazi Irfan
    Ranch Hand
    Posts: 101
    1
    Java Netbeans IDE Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you all of you guys. That cleared things up.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!