Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

finalize()

 
Brian Snyder
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why use finalize()?
 
Siva Prasad
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Brian Snyder
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your honest response. Some things in life just do not make sense!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic