Destroying objects in Java

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 {

  /** The value of this object. */
  private final int value;

  // constructor
  public Money(double x) {
    this.value = x;
  }

  // Should be called when a Person picks up this object.
  public void addTo(Person bob) {
    bob.addToWallet(this.value);
  }

  // MAIN METHOD
  public static void main(String[] args) {
    Person bob = new Person();
    Money dollar = new Money(1.00);
    dollar.addTo(bob);    // bob finds $1.00
  }
}

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;?

+2
8

. JVM, , 99% , , .

.. " - "?

, . . , .

. "" . , , , Collection, List.

List<Money> floorMoney = new LinkedList<Money>();
floorMoney.add(new Money(1.00));

Person, .

Money dollar = floorMoney.get(0); // how you access elements may vary
floorMoney.remove(dollar);
dollar.addTo(bob);

, dollar - addTo, Money , GC'd.

+3

, , , dollar.addTo(), , .

+3

dollar=null . , . .

: , - , . . , , , , ... , , ...

, , .

- Collection (, a List), .

Object , , -1000, , -, .

+1

Java Garbage Collection -GC-, Java .

System.gc() [ Runtime.getRuntime().gc()], , JVM . , JVM , GC . .

, ( , , ) . , 1, null.

DOWNVOTE

Forcing Garbage Collection SCJP . , .

, , , , , .

.. , System.gc() - JVM.

.. JVM, . , JVM ( ).

- , ...

+1

, @pstanton, . , .

public abstract class Possession {
  private Object _key = null;

  public synchronized Object acquire() {
    if (_key != null) { throw new IllegalStateException(); }
    _key = new Object();
  }

  public synchronized void release(Object key) {
    if (_key != key) { throw new IllegalStateException(); }
    _key = null;
  }
}

public class Possessions implements Iterable<Possession> {
  private final Map<Possession, Object> _possessions = new IdentityHashMap<...>();

  public synchronized void add(Possession p) {
    if (!_possessions.containsKey(p)) {
      _possessions.put(p, p.acquire());
    }
  }

  public synchronized void remove(Possession p) {
    Object key = _possessions.remove(p);
    if (key != null) {
      p.release(key);
    }
  }

  public Iterator<Possession> iterator() {
    return Collections.unmodifiableSet(_possessions.keySet()).iterator();
  }
}

public class Money extends Possession { ... }

public class Person {
  private final Possessions _possessions = new Possessions();

  public void add(Money money) {
    _possessions.add(money);
  }
}
+1

, "" , . , GC. , GC (, ++), -, - , , .

0

"selected" default = false? ?

0

( Money s, ) , , .

For example, it may be completely unimportant when a person took the money. And once the money has been taken, the game does not care who owns this instanceMoney

I suggest just adding an attribute isPicked:

public class Money {

  private final int value;
  private boolean _isPicked = false;

  public Money(double x) {
    this.value = x;
  }

  public boolean isPicked(){
    return _isPicked;
  }

  public void addTo(Person bob) {
    if( this.isPicked )
        throw new Exception("some message for developers");

    bob.addToWallet(this.value);
    _isPicked = true;
  }

  public static void main(String[] args) {
    Person bob = new Person();
    Money dollar = new Money(1.00);
    dollar.addTo(bob);    // bob finds $1.00
    // dollar = null;     // don't need this anymore

    Person alice = new Person();
    dollar.addTo( alice ); // <------   RUNTIME EXCEPTION
  }
}

Executing dollar = nullwill also throw an exception, but it will be less secure because you can simply forget to do this or accidentally copy a link to another variable

0
source

All Articles