-1

I would like to know what happens to the old objects when the variable that pointed them before is now pointing another object. Are they deleted or what?

classA var = new classA();
var = new classB();

Is the object "classA" (that was created in the variable "var") deleted when the variable "var" is set equals to the object "classB"?

Guillem Poy
  • 391
  • 5
  • 18
  • I'm sorry but I didn't know about the "garbage collection" before you told me about it. I searched my question and i couldn't find any clear answer with my words. – Guillem Poy Apr 26 '17 at 19:10

1 Answers1

4

When all references to an object cease to exist, it becomes eligible for delition as garbage. Sooner or later, Garbage Collector will remove this object from memory. But that 'later' may not happen at all if there is plenty of memory: there is no guarantee.

Roman Puchkovskiy
  • 11,415
  • 5
  • 36
  • 72