File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes When can we use the method finalize();?? and why do we use it??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "When can we use the method finalize();?? and why do we use it???" Watch "When can we use the method finalize();?? and why do we use it???" New topic
Author

When can we use the method finalize();?? and why do we use it???

Ammar Salem
Ranch Hand

Joined: Dec 29, 2007
Posts: 60
When can we use the method finalize();?? and why do we use it???


****************************<br />For Some Dreams.....<br />One Lifetime Is Not Enough.<br />****************************
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

It is dealt with *garbage collection*. It gives a chance to the object to clean up the resources being used before it is actually garbage collected.

Just before the gc is determined to execute, every object is given a chance to reclaim the resouces being used by invoking the finalize() method.

You are not insisted to use. It is an optional one.

For a quick glance, have a look at the following links.

  • Writing a finalize() method
  • finalize - quick ref.


  • Everything has got its own deadline including one's EGO!
    [CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 37985
        
      22
    I suspect finalize() is there because it is there in C++. It is intended for "cleaning up" resources when an object is deleted by the garbage collector. It is probably only worthwhile using it when there is "native" code; any Java resources like FileReaders should be closed in the method which uses them.

    If you are not using native code, you will hardly ever use finalize(), except for the one good reason for using anything: "I wanted to see what happens if . . ."

    Two articles I have found: No 1, and No 2.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 37985
        
      22
    I think there is a mistake in the finalize method on the website you quoted, Raghavan Muthu. They create the file reading object in one method, then close and set it to null in finalize(). That is a mistake; until finalize() is called, the file is unavailable to other objects.
    Ammar Salem
    Ranch Hand

    Joined: Dec 29, 2007
    Posts: 60
    thank you ranchers !!
    Raghavan Muthu
    Ranch Hand

    Joined: Apr 20, 2006
    Posts: 3344

    Originally posted by Campbell Ritchie:
    I think there is a mistake in the finalize method on the website you quoted, Raghavan Muthu. They create the file reading object in one method, then close and set it to null in finalize(). That is a mistake; until finalize() is called, the file is unavailable to other objects.


    I don't see any mistake there Campbell. If i am right, you are talking about the article which speaks about "Writing a finalize() method". Right?

    It opens a file in the *constructor* and closes and set the reference to null in the finalize() method. what is wrong in it?
    Peter Chase
    Ranch Hand

    Joined: Oct 30, 2001
    Posts: 1970
    Originally posted by Campbell Ritchie:
    It is probably only worthwhile using it when there is "native" code


    That's about right. Using finalisation for anything other than cleaning up memory used by native methods of an object is usually misconceived.

    If you are indeed a beginner (this is the JIG Beginner forum, after all), then you should probably just forget about the existence of finalisation.
    [ January 08, 2008: Message edited by: Peter Chase ]

    Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
    Raghavan Muthu
    Ranch Hand

    Joined: Apr 20, 2006
    Posts: 3344

    That's great. Thank you Peter and Campbell.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 37985
        
      22
    Sorry I haven't been able to reply earlier.

    As far as I remember, one of the examples on a website quoted earlier had a FileReader and BufferedReader instantiated in a method, and closed in the finalize() method. This is almost certainly a mistake because:
  • There is no guarantee that finalize() will be called at a particular time.
  • There is no guarantee that finalize() will be called at all. In which case the file remains locked by the Readers.
  • Even if finalize() is called, there may be a delay of seconds or minutes when the Readers remain open and the file is locked.
  • The correct way to handle FileReaders is to declare them as local variables, and use a finally block to close them:Obviously there are different ways to handle the Exceptions, but this method ensures that the Readers are only open as long as they are needed, and are definitely closed as soon as they are finished with.

    This example also shows how unnecessary the finalize() method is in normal programming.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 37985
        
      22
    . . . and instantiating a FileReader in a constructor is even worse.
    Raghavan Muthu
    Ranch Hand

    Joined: Apr 20, 2006
    Posts: 3344

    That is a *valid* point Campbell.I follow it but here i thought for an example sake it was okay.

    Thanks for pointing it out
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 37985
        
      22
    It might be an example of the sort of thing you can put in finalize() but it actually serves to emphasise how little one actually uses finalize(). You were unlucky picking that particular website.
    Raghavan Muthu
    Ranch Hand

    Joined: Apr 20, 2006
    Posts: 3344

    Originally posted by Campbell Ritchie:
    It might be an example of the sort of thing you can put in finalize() but it actually serves to emphasise how little one actually uses finalize(). You were unlucky picking that particular website.


    Very true CR
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: When can we use the method finalize();?? and why do we use it???
     
    Similar Threads
    Reclaiming memory in java without using garbage collector........
    frnds come up with solution
    finalize method call Error
    about garbage collectiion
    finalize method question!!!