• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

finalize() - OCA JAVA SE I Chapter 1 (Sybex)

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Page 38 of the book :-

finalize()


Just keep in mind that it might not get called and that it definitely wont be called twice !


Can someone clearify the above statement ? I understand the statement that this method cannot be called more than once but doesnt that mean it can be called ?




 
author
Posts: 23836
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"it might not get called" means that there is no guarantee that it will be called.

For example, if you cal System.exit() in your program, your program will exit, and outstanding reachable objects will not get finalized.

Henry
 
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Three things here:

1) The JVM decides when to run garbage collection. You can request that it happens and the JVM may run and it may not run when but you cannot count on it.
2) However even if it does run and your finalize method does get ran it will only get run once.
3) If you finalize method does run but you haven't cleaned up your program correctly the next time garbage collection runs your finalize method WILL NOT get run.

In the end, you have no control over the fact that your finalize method runs. So it's best to either not use it, however if you do then don't put anything it that method that you expect to run all the time. A bad programmer may put something like this in their finalize method and they may not experience any side effects most of the time:



I want to reiterate that the above code is bad and used for example purposes only.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Pete Letkeman wrote:I want to reiterate that the above code is bad and used for example purposes only.


If you post a code snippet (even if it demonstrates bad code), you should make sure that the code compiles successfully (unless you are of course asking a question about a compiler error).

PS. Always use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information. Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers. I already added the code tags for you. See how much easier the code is to read?
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!