Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7898137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:21:08+00:00 2026-06-03T08:21:08+00:00

I have some questions regarding java finalization. For example I have one class FileHelper

  • 0

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..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T08:21:10+00:00Added an answer on June 3, 2026 at 8:21 am

    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

    static void writeFile(String fileName, String text) { // Exception 
     // Open file here 
     // write text
     // close it
    }
    

    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

     FileHelper fileHelper = new FileHelper(file);
     fileHelper.writeFile(text);
     // forgot to fileHelper.close(); 
    

    and you have overridden the finalize and call close() inside when GC runs the file will be closed. The problems:

    • As I mention earlier file should be closed as soon as possible (but not sooner 😉 )
    • It’s indeterministic (No body knows when GC will start)
    • This should be used only to prevent the case when the caller forgets to close the file
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came across this class while reading a C# book and have some questions.
I have some questions regarding reading and writing to CSV files (or if there
I am writing a small application in c++ and I have some questions regarding
I have some general questions regarding the java.util.zip library. What we basically do is
I have some questions regarding dex files What is a dex file in Android?
I've read some documentation on how Adaboost works but have some questions regarding it.
There have been some similar questions asked regarding Grid views, but none have been
I have couple questions regarding some C++ rules. Why am I able to call
I have a question regarding the some data which is being transfered from one
I have some questions about vector ; How can I append one vector in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.