answersLogoWhite

0

Why java does not support destructor?

Updated: 8/9/2023
User Avatar

Zakdaud

Lvl 1
8y ago

Best Answer

AMIT KUMAR

  • 3th Nov , 2014
Java does not support destructors, since java supports the concept of garbage collection,hence the garbage collector is used to automatically free the space which has occupied by the program while running.Garbage collector calls the finalize() method which is defined in the object class. finalize() method is called once for each object.
User Avatar

Wiki User

8y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

The finalize() method in Java is the closest mechanism to the concept of a destructor. But since the garbage collector is not guaranteed to run at any specific time depending on finalize() to cleanup resources is not best practice.

The finalize() method in java objects is called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

Best practice in Java is to close resources and release references to them when they are no longer needed so the garbage collector can reclaim memory. So for example, InputStreams have a close() method which should be called in a finally block to ensure the streams are closed.

Java 1.7 introduced the try-with-resources statement that declares one or more resources and closes the resource after the statement block is executed even if an exception occurs. This a destructor-like mechanism but must be explicitly called in the program.

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

The purpose of a Destructor is usually to clear off unused variables and clean up the memory. Java has in built memory handling mechanisms (Garbage collection) that clear off unused memory automatically. Hence there is no requirement for destructor methods.

Tip: In spite of automatic garbage collection by java, it is a good practice to nullify all unused objects in the finally block of the code.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why java does not support destructor?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Is destructor allowed in java?

No. Java does not support the concept of Destructors like C


How does java support the concept of destructer in java?

In Java, Java does support the concept of destructor, it's done via special method finalize.


What does java not support?

Java does not support multiple inheritance.......


What do you use instead of a destructor in java?

Hi, This is suneetha. We use Finalize() method in java instead of destructors. Finalize() is used to release the memory before the garabage collection takes place.


Does a DSI have java?

No it does not support Java


Does java support call by reference?

No , Java does not support call by reference.


Does Java support copy constructor?

No. Java does not support copy constructor


Does the G1's browser support Java and Flash?

No. I have read that it will support Java. I believe that it does not support flash ATM.


What are the background pointers in java?

Java does not support pointers.


What is Java Support Systems?

Java support system includes: · Applets · Servlets · Java Beans · EJB · JSP · XML · SOAP · CORBA


Can you use pointers in java?

Java does not support Pointers and hence you cannot use it in Java.


Does Linux support Java?

Yes. Linux is fully supported by Java.