
How can i call destructor in java using System.gc() or manually. Please anyone give me a example program

Java is a garbage collected language therefore you cannot predict when (or even if) an object will be destroyed. Hence there is no direct equivalent of a destructor.
There is an inherited method called finalize for Garbage Collection.
import java.io.*;
public class FinalizeObject extends FileOutputStream {
// Contructor definition of the FinalizeFileOutputStream class.
public FinalizeFileOutputStream() throws IOException{
super("Rose India"); // call the super class constructor.
}
public static void main(String[] args) throws IOException {
// Create object of FinalizeFileOutputStream class.
FinalizeFileOutputStream objOutstream = new FinalizeFileOutputStream();
// finalize() method call.
objOutstream.finalize();
System.out.print("Stream is closed successfully and connection has "+
"cleaned up to the file.");
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.