#Destroy objects ?

22 messages · Page 1 of 1 (latest)

wide olive
#

Hello,

I would like to know how to explicitly destroy objects in Java.
In university projects I found that we override the function selfDestroy:

@Override
public void selfDestroy() {
    current = null;
}

However, I can't find this anywhere on the internet.

P.S.: I know that Java is a language with GC.

night sentinelBOT
#

This post has been reserved for your question.

Hey @wide olive! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

rose lynx
#

There's autocloseable interface you could implement

#

This is done by IO stuff to close the underlying resources

#

But as long as it's standard java objects, you can just ignore it, the gc will take care of it

wide olive
#

Yeah but that's IO, in this case we're trying to implement a composition as described in UML

rose lynx
#

You can set any property to null, as long as it's not final

#

But I might be misunderstanding the question. Do you have a practical example?

wide olive
#

The teacher's answer is to put, in this case, B as a inner class for A

rose lynx
#

No, those were finalizers and those are deprecated

#

In Java you don't have to remove references

#

Just don't reference the object anymore

#

The garbage collector will take care of the rest

wide olive
#

Cool, thanks for the clarification =)

night sentinelBOT
# wide olive Cool, thanks for the clarification =)

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.

rose lynx
#

Yeah, so if you don't need A anymore, you dont need to set a.b to null

#

Just ignore a, and Java does the rest 🙂

wide olive
#

Imma close the post

#

thanks again :)