I have a general idea of โโhow the Garbage Collector works in Java, but my argument for destroying an object is not because I care about freeing memory, but because of the functionality. I can better explain with an example:
Say I'm making a game in which money is involved. When a person takes an object Moneyfrom the ground, I want to call this object a addTomethod that involves adding a value to this wallet.
public class Money {
private final int value;
public Money(double x) {
this.value = x;
}
public void addTo(Person bob) {
bob.addToWallet(this.value);
}
public static void main(String[] args) {
Person bob = new Person();
Money dollar = new Money(1.00);
dollar.addTo(bob);
}
}
Once it dollaris found, I do not want anyone else to pick it up. In other words, I do not want me or any other program to accidentally call a line:
dollar.addTo(alice);
, , , , . , , , , - . dollar, dollar = null;?