Douglas
Douglas

Reputation: 11

Deleting Objects in Java

So in my program I have several different objects stored into an array. I randomly generate a number that is an index into the array and clone that object (nine times) to make a 3x3 grid of objects on my screen.

Now my problem is after I have my 3x3 grid I want to be able to erase that and generate a new grid of objects in that are in the array. The problem is I can't think of how to get rid of all these clones except for just moving them out of the display which seems like a waste of memory. I want to do like 400 trials, so that would be a lot of cloned objects by the end.

Is there a way I can delete these cloned objects? I have to create new objects, because it is possible that one of the objects in my array gets used twice in the grid.

Upvotes: 1

Views: 3218

Answers (3)

phill
phill

Reputation: 486

It helps to realize that you aren't dealing with actual objects, you are dealing with pointers or references to objects in memory. So:

Object obj = new Object();

is not an object but a link to an object in memory. Removing the link by setting it to null or replacing it with a link to another object, the old object is deleted...ish. When garbage collection runs, it is deleted. So:

obj = null;

and

obj = new Object();

will essentially delete the old object. The same applies to an array of objects, you aren't dealing with an array of actual objects but an array of links to objects. So by setting a position in an array to null or setting a position to a new object, the old object is deleted. So:

ObjArray[4] = null;

and

ObjArray[4] = new Object;

Will delete the old object.

Edit: Something I guess I forgot to mention, A single object in memory can have multiple links to it and it is not until all of those links are remove that the object is in a position to be deleted by garbage collection.

Object obj = new Object();
ObjArray[4] = obj;
obj=null;

In the above case, the object that was originally created will not be deleted when obj is set to null, this is because a link to it still exists in the ObjArray. You have to set both obj and ObjArray[4] to null before the object is gone.

Upvotes: 0

Simone-Cu
Simone-Cu

Reputation: 1129

Because the reference still remains inside the container. I think you have done anything like: myJFrameObject.getContentPane.add(object_to_display); so the reference is inside myJFrameObject, and you can use myJFrameObject.getContentPane.remove for example. If you want you can use setVisible(false) to hide the frame. It depends on the situation.

Upvotes: 0

Simone-Cu
Simone-Cu

Reputation: 1129

I think java garbage collector will do it for you. When an object isn't referenced from anyone it will be deleted when the system recognize this; but what do you mean when you say "move them out of the display"? If they are "graphic object" they should be inside other object (like JFrame) so you have to use a method to tell the JFrame object (or other) to throw the reference away or overwrite the reference with another one.

Upvotes: 2

Related Questions