• Post Reply Bookmark Topic Watch Topic
  • New Topic

finalize method  RSS feed

 
Ganesh Kumar
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whats the use of finalize methods???
Will we call that method???


 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An object's finalize() method is called by the garbage collector before an object is discarded. This provides a way for an object to do some "cleanup" at the end of its life. Since there's no guarantee that any particular object will ever actually be garbage-collected, this is pretty unreliable, and in truth it's not used very often. You can't really rely on it.

No, you should never call finalize, unless you're overriding it, in which case you should call super.finalize().
 
Juggy Obhi
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am unable to figure out what is the Alternative to Finalize()....since finalise() is unpredicatable in the sence you can never be sure it will be called or not....So

Where Exactly to Put your code that Releases Resources back to the system?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most of the time variable scoping is enough control to release resources. That is, when all variables that refer to a particular object instance go out of scope, the JVM garbage collector can collect the object, and that usually frees up important resources. There can be exceptions when the resources are outside the JVM, perhaps in native code called by JNI or other processes like databases.

Are you comfortable with how Java resources are released? This automatic memory management to avoid the potential for mistakes that you have in C/C++ was one of the reasons Java was invented.

Or are you concerned about the trickier ones outside that model?
[ July 08, 2007: Message edited by: Stan James ]
 
chintan ramavat
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it necessary to give a call to super.finalize() if you override the finalize() method. or not so, cause compile-time error.

- thanks
 
Juggy Obhi
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wanted to ask...for example if in a function you have opened a file/database connection....will that be released/closed automatically after the end of that function?
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Juggy obhi:
I wanted to ask...for example if in a function you have opened a file/database connection....will that be released/closed automatically after the end of that function?


No, it won't. It probably will get automatically released when the associated objects get GC'd, but that could happen a long time later.

You should always explicitly close files and database connections when you are finished with them. Don't rely on GC to do it; that's not GC's job (it is responsible for memory resources only).

The "finally" clause is useful for ensuring that files etc. get closed even when an exception is thrown.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!