1

I have a HashMap that stores instances of a Class.

I create the instances by putting them directly into the HashMap like so:

public HashMap<String, Object> map = new HashMap<>();

public void guardarFicha(String ID, Object instanceOfClass) {
    map.put(ID, instanceOfClass);
}

Is it enough to permanently delete the instance by removing its reference in the list? Like this:

public void delete(String ID) {
    this.map.remove(ID);
}

Or do I need to set each instance variable of that instance to null?

Thx in advance!

juan fran
  • 359
  • 3
  • 12
  • 1
    `"Or do I also need to declare null the instance itself?"` -- doesn't make since since an instance == an object and can never be null. I assume that you mean instance *reference variable*, and suggests that you will want to read similar questions about getting objects GC'd. – Hovercraft Full Of Eels Jun 07 '18 at 22:15
  • 1
    As a side note, naming a variable `list` when it holds a reference to a `HashMap` is likely not a good idea. Readers of your code may think `list` is of type `List`. – Logan Jun 07 '18 at 22:16
  • Hovercraft Full Of Eels, I rephrased the question since I believe it is more specific than "how to destroy an object in java?", and I still have the same doubt. When I delete the instance from the HashMap (where the instance itself has been declared), does it become eligible for the Garbage Collector? – juan fran Jun 07 '18 at 22:33

0 Answers0