Stan
Stan

Reputation: 3699

How do I delete an object in java?

I want to delete an object I created, (e.g. an oval that follows you), but how would I do this? I ran the statement:

delete follower1;

but it didn't work.

Background:

I'm making a small game with a oval you can control, and a oval which follows you. Now I've got files named: DrawPanel.class, this class draws everything on the screen, and handles collisions, sounds, etc. I also have enemy.class, which is the oval following the player. I also have entity.class, which is the player you can control. And if the player intersects with the follower (the enemy), I want my player object to get deleted. Currently, the way I'm doing it is:

public void checkCollisions(){
    if(player.getBounds().intersects(follower1.getBounds())){
        Follower1Alive = false;
        player.health = player.health - 10;
     }
 }

Upvotes: 120

Views: 504055

Answers (7)

Jake Kalstad
Jake Kalstad

Reputation: 2065

Java has a Garbage Collector, it will delete the object for you if no reference is held to it anymore.

Upvotes: -2

MByD
MByD

Reputation: 137322

You should remove the references to it by assigning null or leaving the block where it was declared. After that, it will be automatically deleted by the garbage collector (not immediately, but eventually).

Example 1:

Object a = new Object();
a = null; // after this, if there is no reference to the object,
          // it will be deleted by the garbage collector

Example 2:

if (something) {
    Object o = new Object(); 
} // as you leave the block, the reference is deleted.
  // Later on, the garbage collector will delete the object itself.

Not something that you are currently looking for, but FYI: you can invoke the garbage collector with the call System.gc()

Upvotes: 213

Lakhan
Lakhan

Reputation: 73

You can remove the reference using null.

Let's say You have class A:

A a = new A();
a=null;

last statement will remove the reference of the object a and that object will be "garbage collected" by JVM. It is one of the easiest ways to do this.

Upvotes: 1

Brian Roach
Brian Roach

Reputation: 76898

Your C++ is showing.

There is no delete in java, and all objects are created on the heap. The JVM has a garbage collector that relies on reference counts.

Once there are no more references to an object, it becomes available for collection by the garbage collector.

myObject = null may not do it; for example:

Foo myObject = new Foo(); // 1 reference
Foo myOtherObject = myObject; // 2 references
myObject = null; // 1 reference

All this does is set the reference myObject to null, it does not affect the object myObject once pointed to except to simply decrement the reference count by 1. Since myOtherObject still refers to that object, it is not yet available to be collected.

Upvotes: 83

user3124852
user3124852

Reputation: 1

//Just use a List
//create the list
public final List<Object> myObjects;

//instantiate the list
myObjects = new ArrayList<Object>();

//add objects to the list
Object object = myObject;
myObjects.add(object);

//remove the object calling this method if you have more than 1 objects still works with 1
//object too.

private void removeObject(){
int len = myObjects.size();
for(int i = 0;i<len; i++){
Objects object = myObjects.get(i);
myObjects.remove(object);
}
}

Upvotes: -8

fmucar
fmucar

Reputation: 14558

You don't need to delete objects in java. When there is no reference to an object, it will be collected by the garbage collector automatically.

Upvotes: 4

John Percival Hackworth
John Percival Hackworth

Reputation: 11531

If you want help an object go away, set its reference to null.

String x = "sadfasdfasd";
// do stuff
x = null;

Setting reference to null will make it more likely that the object will be garbage collected, as long as there are no other references to the object.

Upvotes: 15

Related Questions