Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
in Java by (3.4k points)
I need to know when the finalize() method is called in the JVM. I created a test class which writes into a file when the finalize() method is called by overriding it. It is not executed. Can anybody tell me the reason why it is not executing?

1 Answer

0 votes
by (46k points)

In usual, it's enough not to depend on finalize() to do any sweeping up, etc.

According to the Javadoc (which it would be meriting study), it is:

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

This may nevermore occur in the life of a program if the object is constantly accessible.

Additionally, the garbage collector isn't guaranteed to run at any particular time. In common, what I'm trying to say is finalize()is not the most reliable approach to use in general unless there's something definite you need it for.

Related questions

0 votes
1 answer
asked Oct 27, 2019 in Java by Shubham (3.9k points)
0 votes
1 answer
asked Nov 25, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Nov 13, 2019 in Java by Nigam (4k points)

Browse Categories

...