Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

How are finalizers different from C++ destructors?

destructors different
0
Posted

How are finalizers different from C++ destructors?

0

Java programming language objects are not explicitly deleted and do not have destructors. Instead they are implicitly garbage collected when the JVM realizes your program can no longer access them. This technology is not based on reference counting and will cope with circular references. Every object has a routine called finalize() which will be called before the object is collected. This is the nearest equivalent to C++’s destructor. However, it is not a good idea to rely on finalization for the timely freeing of resources. This is because garbage collection and hence finalization may be arbitrarily delayed, and may never happen at all if the program terminates before it runs out of memory. You should instead provide your objects with methods similar to Graphics.dispose() to free resources, and call the dispose() method explicitly when you have finished using them – typically within the “finally” clause of a “try/catch” block. You may then call your dispose() method from within your f

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123