0

How do I delete a java object from it' class. Let's say I have a class like:

class A
{
    int hp;
    public void update()
    {
        if(hp <= 0)
        {
            //here comes the problem. Something like: this.delete(); ??
        }
    }
}

and I have an instance in my main method that checks if it should delete itself.

EDIT: In my case 'A' is a windmill class in a 2D RTS. It sits in an ArrayList of windmills and has following methods:

init(called when the game starts to load images)

update(this is where i want to delete the object) 

draw(for rendering)
29
  • Please define "delete"? Please provide some context, any context. Commented Jan 20, 2014 at 22:26
  • sth like free up the memory? Commented Jan 20, 2014 at 22:27
  • 1
    I think you guys are misinterpreting his question. Look at the context from the minimal code he has provided. Assuming hp stands for hitpoints, it appears he is trying to manage a Collection of Entitys, perhaps he wants to remove it from his Collection... I don't know. Commented Jan 20, 2014 at 22:32
  • 1
    An object can be freed after there is not more strong references to that object. i.e. it is deleted when you don't need it any more. Commented Jan 20, 2014 at 22:34
  • 2
    @HongWeiWang You can set a reference to null. Not an object. And nulling references is mostly a waste of time. Commented Jan 20, 2014 at 22:35

3 Answers 3

1

In Java, you cannot explicitly delete an object from memory. The nice thing is that this is handled by what we call the "Garbage Collector". When this runs, it goes around finding objects with no references to them and deletes them. Thus, the only thing you can do is remove all references to the object (let them go out of scope, set them to null, whatever) and then wait for the GC to run.

There is no way to force the GC to run, however, you can technically "encourage" it to run with the System.gc() method. This does not promise that it will run however and is not needed in anything other than edge cases.

Sign up to request clarification or add additional context in comments.

Comments

1

You're trying to "free up memory" of an object, and unfortunately there is no one-size-fits-all solution. You must null all active references to the object. Again this all depends on context, on what variables refer to your object of interest. Do this and the GC will free the reference, when it decides that GC'ing needs to be done, something we don't have control over, but only can make suggestions to the GC.


you state in comment:

lets say A is a sodier and if it gets killed I wand to delete him

Then remove him from the soldier duty list, possibly a database or ArrayList.


Best to declare variables in as limited a scope as possible, to have them naturally be freed from all references when no longer needed, and to profile your code so that you can see when it is running into trouble.


You state in comment:

yes the object is in an ArrayList which luckily is public and static so the problem is solved. thans for your broad explanation

No, this is not correct. The ArrayList should not be static nor public but rather should be a private instance field. Making the field static means that it can not be used in an object-oriented way. Making it public means that any object can mutate it at any time, and in complex programs this can lead to difficult to trace problems and increased cyclomatic complexity. It should be mutable only in a controlled way via public methods of the class that holds it. I'm sorry, but your program design sounds flawed suggesting that you will want to review, refactor, improve it. How, it's hard to say based on snippets of information.

7 Comments

I haven't thought about it, thanks!
Terminology please. Null all references ... and GC will free the object.
@user2466076 even one reachable, strong reference will keep an object alive as it still might be needed.
@user2466076 (a) No. (b) Yes. You've been told all this several times.
the ArrayList is static because its a universal list of instances. It is located in an external file. I use it on many different ocasions so i need it public
|
0

Even if C++ you have to clean up your pointers/references to an object before you can safely free it's memory. In Java, this is all you have to do and it does the rest.

update(this is where i want to delete the object)

This means you want to remove it from your ArrayList. You can either

  • do it immediately, but this requires a reference to the list(s) the object is in.
  • or remove dead entries on discovery e.g. when iterating over the list.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.