• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

basic collection question

 
Brian Smith
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is just a general question

Is this valid?



Is the element o in the arrayList also dereferenced when I set o = null? Or is the object copied when added to the list?

Thanks
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The variable o is a reference to an object. The list has another reference to the same object. When you set o to null or let it go out of scope, the list still has its reference, so the object cannot be garbage collected yet. Did that answer the right question?

BTW: Does that compile? If not, can you fix it?
 
Brian Smith
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know if it compiles, it was just "air code" to convey my question. Yes that answers my question, thanks. I didn't think it through enough before I posted. I was getting confused between setting an object reference to null and setting the value of the object to null. The answer is obvious now, Thanks.

-Brian
 
Brian Smith
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok this compiles (Java 1.5)

 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad you followed it through to compiling. I suspected that your question didn't really require it, but it's cooler this way.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic