Destroying objects in Java

后端 未结 8 768
走了就别回头了
走了就别回头了 2021-01-22 00:07

I have a general idea of how the Garbage Collector works in Java, but my reasoning for destroying an object is not because I care about freeing up memory, but because of functio

8条回答
  •  醉话见心
    2021-01-22 00:51

    dollar=null will do the trick. By eliminating the reference to that object, the object cannot be referenced. The garbage collector is not relevant here.

    Think about it this way: if you don't want someone to come to your house, you don't have to blow up your house. Just don't give them the address. Fortunately Objects can't stalk other Objects like a crazed ex-girlfriend can stalk the guy who dumped her...parking outside his work, sending texts at all hours, calling his parents...

    Oh, sorry, back to your question.

    If the ground is a Collection of some sort (e.g. a List) then removing that money from your ground collection will also keep it out of the reach of other players or rogue sentient Objects.

    And if your money is also an Object with a value, after picking the first player picks it up you could set its value to -1000, thus punishing the players who would take something that is clearly not theirs.

提交回复
热议问题