Java MCQ: Which of the following is true about the ‘finalize()’ method in Java?
Answer:
Explanation:
The finalize()
method in Java is called by the garbage collector just before an object is destroyed. It provides an opportunity for the object to clean up resources, such as closing files or releasing memory, before it is removed from memory. However, the finalize()
method is generally discouraged because it is unpredictable and can lead to performance issues.
Since the introduction of more reliable and efficient resource management techniques like try-with-resources, the need for finalize()
has diminished. Additionally, as of Java 9, the finalize()
method has been deprecated, and developers are encouraged to use alternative resource management strategies.
Understanding the limitations and appropriate use cases for finalize()
is important for writing efficient and reliable Java programs.
Reference links:
https://www.rameshfadatare.com/learn-java-programming/
https://www.javaguides.net/p/java-tutorial-learn-java-programming.html