Home TechnologyProgrammingJava Subscribe to RSS

Can We Write Destructors Explicity In Java Language?

Answer Question

2 Answers - Sort by: Date | Rating

    Yes we could. As we use constructor, so is the destructor.
    0 0

    Swetha1992 

    answered 3 years ago

      Java's garbage collection mechanism automatically detects when there are no further references to an object and reclaims the memory associated with it. There is no need to explicitly clean up memory as is done with destructors in the C++ language.

      If your object has some resources associated with it, however, and you feel the need to process these in some way before your object is destroyed you can override the finalize() method in your class. All Java classes have a finalize() method inherited from the Object class. Normally, this it is not necessary to override it.

      In some cases, as, for example, if the object opens files and you want to make sure they are properly closed before the object itself is destroyed, you could implement the finalize() method. The garbage collector will always call it before destroying your object. It is considered good form to call super.finalize() after your own code is done so that superclasses can perform any finalization of their own.
      0 0

      Cian 

      answered 3 years ago

        More

           
           

          Ask a Question via Twitter

          Send a question to @askblurtit and we will publish it online and send you a reply everytime you receive an answer.

          Blurtit Store

          Get T-shirts, hoodies, caps and more at the Blurtit store