Difference between final, finally and finalize() November 8, 2006
Posted by :) in JAVA.trackback
- final – constant declaration.
- finally – The finally block always executes when the try block exits, except System.exit(0) call. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.
- finalize() – method helps in garbage collection. A method that is invoked before an object is discarded by the garbage collector, allowing it to clean up its state. Should not be used to release non-memory resources like file handles, sockets, database connections etc because Java has only a finite number of these resources and you do not know when the garbage collection is going to kick in to release these non-memory resources through the finalize() method.


finally will not handle any exceptions.Exceptions are handled by catch block and can be reported with the help of “throws” keyword.Finally block is used to release resources irrespective of exceptions; as opening a resource is a costlier process the programmer must take necessary steps to release the resources which he has opened, this can be achieved with the help of finally.
Informative
tell me more about final
kindly explain me with some more examples
sir i have some more doubts in java will pls give me the answers for that.
and my questions are in java why we should always assign instance variables to local variables only
2.In java we can create an objects for only derived classes why/
Are you saying we should never call the finalize() method in our program and that we should let the garbage collector decide when to freeup/collect unused resources?
Could you please provide some examples of where finalize() should be used and where it should not be?
Thanks
[...] Difference between final, finally and finalize() [...]
final is a keyword. its constant. it cant be changed from its
initiated value.
Finally() method used exception handling concept. finally () block will execute whether or not try block can be execute. Its used to close a file.
finalize is used when an object is just before deleted, it can be used in garbage collection.
hi,
could you please tell me in detail about final, finally, finalize.
Thanks
[...] Difference B/w Finalize and Finally in .Net? Difference between final, finally and finalize() « ALLU __________________ Honor and [...]
Hello webmaster
I would like to share with you a link to your site
write me here preonrelt@mail.ru
sir
could u say the exact meaning of final finally and finalize() method…which even the beginners can understand
After Reading the whole stuff I just thought to put it in other way, Lets forget about JAVA here.
Final : I wanted to have a dog and only the one I have seen named Tomy, never anyother of such kinds in my life. so its final.
Finaly : I am going for shopping, does not matter what I try and buy and what I can,t . Finaly after paying bill I should drop my Shoping Cart in the proper place.
Finalize: If some one trying to kill me , let me kill my all dogs first, because there is no one who can take care of them and they will be mistreated.
very nice sanjeev…
Nice One Sanjeev