aspose file tools*
The moose likes Beginning Java and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark ""finally block" and "finalize()"" Watch ""finally block" and "finalize()"" New topic
Author

"finally block" and "finalize()"

lucy hu
Ranch Hand

Joined: Aug 21, 2000
Posts: 60
I am confused by these two,
are they both ok for cleaning up code?
what is the difference betwn them?
thank you!
Mohan Guru
Greenhorn

Joined: Nov 08, 2000
Posts: 8
finalize()is associated with Garbage collector(gc).
Reallocation of memory is automatic in Java,which is performed
by the gc.
finally block is with exceptional handling .It is used with try/catch or try block.
Generally,we have to use it for that purpose only.That will be useful to maintain the clarity.
John Wetherbie
Rancher

Joined: Apr 05, 2000
Posts: 1449
As noted above finalize() is associated with garbage collection and can be used to clean up resources. You may not want to depend on finalize to clean something up though, since there is no guarantee that the object will be garbage collected. You may need/want to write your own resource (non-Java memory) cleanup method.
The important thing to remember about a finally block is that it is always invoked. Don't put code in there that you only want called if an exception occurs. You may cleanup some resources you're about to use!


The only reason for time is so that everything doesn't happen all at once.
- Buckaroo Banzai
lucy hu
Ranch Hand

Joined: Aug 21, 2000
Posts: 60
if the finally is in the finalize() method, is it guranteed to be executed still?
like the following code:
ObjectOutputstream objStorage;
//...
protected void finalize() throws Throwable {
try {
if (objStorage != null) objStorage.close();
} finally {
super.finalize();
}
}
Mohan Guru
Greenhorn

Joined: Nov 08, 2000
Posts: 8
hai!
finalize() is called only at the time when the gc is about to
reclaim an object with no refrence in any part of the program.
so then only the finally block will be executed.
John Wetherbie
Rancher

Joined: Apr 05, 2000
Posts: 1449
Right!
Just remember, you can't guarantee an object will be gc'd and also note that depending on what is in your finalize method you could cause the object to be referenced again. Now your object won't be gc'd and finalize won't run again (per the spec it runs once and only once).
BTW, I think it would be safest in your example code to call the base class finalize first.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Just a reminder... calling finalize() does not cause garbage collection to be invoked. finalize() is called when the system invokes garbage collection for an object. Since an object may not ever be garbage collected, you should not rely on finalize() to release resources.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: "finally block" and "finalize()"