wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes finalize() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "finalize()" Watch "finalize()" New topic
Author

finalize()

Brian Snyder
Ranch Hand

Joined: Feb 03, 2001
Posts: 142
Why use finalize()?
Siva Prasad
Ranch Hand

Joined: Feb 22, 2001
Posts: 104
finalize() is mostly used when working native methods.
It should be used only when you want to cleanup the memory not allocated by Garbage Collector.
Correct me if I am wrong.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
True, but it applies to more things than just native methods. For instance Graphics Contexts (anything with a native peer) in the AWT are not gc'd and should be specifically disposed() in a finalize method. If you have many threads running you can terminate them in a finalize.
You might use finalize to insure that a socket is closed, or if you are writing an Stream you might insure that it gets closed in finalize.
But also notice that most of this kind of stuff is already done in the classes that Sun provides, and if we extend those classes we inherit the finalize method with it. So for the most part (should I say this?? ), we will not need to code our own finalize.
[This message has been edited by Cindy Glass (edited March 24, 2001).]


"JavaRanch, where the deer and the Certified play" - David O'Meara
Brian Snyder
Ranch Hand

Joined: Feb 03, 2001
Posts: 142
Thanks Siva and Cindy.
Cindy, I agree with your final statement. This is a topic that is troubling me for the SJP2 exam. Why even test on it if you don;t really need it???
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
In case you go to work for Sun some day, and YOU are the guy coding the Foundation Classes .
Or maybe the company hiring you is the creator of Libraries for sale and they have to be as solid as what Sun puts out.
etc.
Brian Snyder
Ranch Hand

Joined: Feb 03, 2001
Posts: 142
Thanks for your honest response. Some things in life just do not make sense!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: finalize()