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; } }
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.
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.
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.
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.
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()
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With