Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object memory

 
Aaron Webb
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I do...
SomeObject obj = new SomeObject();
obj = new SomeObject();
...what happens in memory? Does the new object replace the first one?
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aaron
What will happen is that the variable obj will refer to the newly created object of type SomeObject. The original object that it refered to will become eligible for garbage collection and at some point the GC will destroy it and reclaim the memory that it used.
The new object doesn't replace the other one in memory the location of the new one will replace the location of the original one in the varialbe obj.
Did that answer it for you?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic