Deleting an object in java?

Java

Java Problem Overview


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;
	}
}

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

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.

Solution 3 - Java

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.

Solution 4 - Java

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.

Solution 5 - Java

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.

Solution 6 - Java

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

Solution 7 - Java

//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);
}
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStanView Question on Stackoverflow
Solution 1 - JavaMByDView Answer on Stackoverflow
Solution 2 - JavaBrian RoachView Answer on Stackoverflow
Solution 3 - JavaJohn Percival HackworthView Answer on Stackoverflow
Solution 4 - JavafmucarView Answer on Stackoverflow
Solution 5 - JavaLakhanView Answer on Stackoverflow
Solution 6 - JavaJake KalstadView Answer on Stackoverflow
Solution 7 - Javauser3124852View Answer on Stackoverflow