Solved Deleting a class instance?

Discussion in 'Plugin Development' started by BagduFagdu, Jul 25, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    BagduFagdu

    So, I have this custom player class, made one so that I could add custom methods and stuff. However, I don't know how to come up on how to delete it or make a method to delete it.

    This is an example:

    Code:
    public class TUser {
    
      private Player player;
      private int tokens = 0;
    
      public TUser(final Player player, final int tokens) {
       this.player = player;
       this.tokens = tokens;
      }
    
      public Player getPlayer() {
        return player;
      }
      
      public Integer getTokens() {
        return tokens;
      }
      
      public void delete() {
        // This is my question, do I nullify the values or what else?
      }
    
    }
     
  2. Offline

    Hawktasard

    @BagduFagdu
    Set the values to null (<- optional) and remove the TUser from the collection it's stored in.
     
    Last edited: Jul 25, 2015
    BagduFagdu likes this.
  3. Offline

    Konato_K

    @BagduFagdu Java is a garbage-collected language, so you can't just "delete" something, however, if an object does not have any hard reference leading to it then it will eventually be garbage collected.
     
    BagduFagdu likes this.
  4. Offline

    1Rogue

    Ammending to the above: once the object is off the stack (any amount), it will be gc'd. Meaning if you have a reference to something way up on the hierarchy, with a lot of references inside of it, all of it will be gc'd (if there are no other hard references to content within it active on the stack).
     
    BagduFagdu likes this.
  5. Offline

    xTrollxDudex

    1. WeakReference "player" please
    2. Int will not need to be nullified

    There are a few things in Java that makes this easy/difficult. As previously said, you don't need to remove anything, just make sure you have no strong references to the instance of TUser. Java is memory managed, meaning that you don't need to release resources you don't need, just don't have any strong references to your object.

    If you want some more technical detail, read on (likely will confuse you): You cannot force explicit collection (at least in Oracle VMs). You cannot reliably release a resource consistently yourself. You could probably write a null byte array to the memory pointer, but keep in mind that these pointers are rearrange and organized differently by the GC, doing so could seriously damage the program state. In short: trust the GC to do its job, do your job and release the strong references.
     
Thread Status:
Not open for further replies.

Share This Page