java - How to make object null by having only its reference in array? -


i have array of game objects in gameworld , can removed world. problem game objects have references other game objects. e.g. player class has reference bird. bird gets randomly removed gameworld, player still has reference it. null check check whether gameobject still valid , in world. however, removing object array not make reference null. how can make null?

here example:

// gameworld creates bird arraylist<object> gameobjects = new arraylist<>(); object bird = new object(); gameobjects.add(bird);  // player references object referencedbird = gameobjects.get(0);  // later in gameworld, scope, there no access 'bird' object, trying remove bird world object objecttoremove = gameobjects.get(0); gameobjects.remove(0); objecttoremove = null;  // in player class debug.log("is null " + (referencedbird == null)); // false! need true 

you can't make object null, can make reference null. updating 1 reference object doesn't change other references same object.

think both having same person's number in our phones: if delete number, doesn't deleted phone. , if both delete it, person's phone number doesn't cease exist: neither of can call them.

the things can are:

  • set referencedbird = null explicitly;
  • or refer via gameobjects.get(0) instead of via referencedbird variable.

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -