Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting an object in java?

Tags:

java

I want to delete an object I created, (a oval which follows you), but how would I do this?

delete follower1; 

didn't work.

EDIT:

Okay, I'll give some more context. 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 got an enemy.class, which is the oval following the player. I got an entity.class, which is the player you can control. And if the player intersects with the follower, I want my player object to get deleted. The way I'm doing it:

    public void checkCollisions(){     if(player.getBounds().intersects(follower1.getBounds())){         Follower1Alive = false;         player.health = player.health - 10;     } } 
like image 595
Stan Avatar asked Apr 22 '11 16:04

Stan


People also ask

How do you delete an object in Java?

You can delete an object in Java by removing the reference to it by assigning null. After that, it will be automatically deleted by the Garbage Collector. Just assign a null value to it, if you have the object in a collection the collection might have a method to delete it.

How do you delete an object?

Right-click over the objects you want to delete, and choose Delete. In the Delete dialog box, select the objects you want to delete from the list.

How do you delete an object from a class?

When delete is used to deallocate memory for a C++ class object, the object's destructor is called before the object's memory is deallocated (if the object has a destructor). If the operand to the delete operator is a modifiable l-value, its value is undefined after the object is deleted.

How do you remove one object from a list in Java?

The remove(Object obj) method of List interface in Java is used to remove the first occurrence of the specified element obj from this List if it is present in the List. Parameters: It accepts a single parameter obj of List type which represents the element to be removed from the given List.


2 Answers

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()

like image 88
MByD Avatar answered Oct 20 '22 06:10

MByD


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.

like image 43
Brian Roach Avatar answered Oct 20 '22 07:10

Brian Roach