• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When can we use the method finalize();?? and why do we use it???
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

  •  
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 60
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    thank you ranchers !!
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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?
     
    Ranch Hand
    Posts: 1970
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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 ]
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    That's great. Thank you Peter and Campbell.
     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    . . . and instantiating a FileReader in a constructor is even worse.
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic