I have some questions regarding java finalization.
For example I have one class FileHelper and this class is associated with reading file,writing file,etc.
Now my question is, I have one method writeFile() in FileHelper class.Now If I want to close that file
should I override the finalize() method and close the file or can I close the file inside the writeFile() method itself? Which is the right way to do? I have declared my File variable as a member variable. If overriding is a bad idea, then why do we want to override finalize() method? which scenario? I have read many articles,where they are saying to close system resources such as file,font etc..
I have some questions regarding java finalization. For example I have one class FileHelper
Share
The best practice is to close the file as soon as possible. If you have static method in FileHelper (assuming that your Helper is a bunch of static “helper” methods) I would close the file inside
The purpose of overriding finalize is release the unmanaged resources e.g. files in case someone forget to do it. If someone use your helper this way
and you have overridden the finalize and call close() inside when GC runs the file will be closed. The problems: