jump to navigation

Difference between final, finally and finalize() November 8, 2006

Posted by Allu 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.
About these ads

Comments»

1. abhishek goud - November 24, 2006

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.

Aadhi goud - November 14, 2010

what is enum and generics?

veda - July 8, 2012

what u said is true but we write lines to release but it is not the only use … main thing about finally block is it will execute no matter what the exception is or not …what ever code u right in it … try writing divide by zero in try and catch and add two numbers in finally two numbers will be added ..

Gaurav Gupta - August 29, 2012

its true veda…please give some more example & usage…
Gaurav—-09819671291

2. jag - July 19, 2007

Informative

3. sugam - September 6, 2007

tell me more about final

4. vinoth - September 26, 2007

kindly explain me with some more examples

5. hemalatha - October 1, 2007

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/

siva - October 24, 2011

you can create the object for base class also. It is possible for accessing the object inside the class. Because object is the instance of a class and it is not only belongs to only to derived class.

ravi - March 16, 2012

kutti kamini

sangi - August 28, 2012

2)by creating objects in dervied class we can acess all features base classes thats we should create objects in dervied class otherwise we can”t acess all features……..

6. Vidya - August 27, 2008

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

7. Java interview Questions « Java Stuff - September 13, 2008

[...] Difference between final, finally and finalize() [...]

sravan.vinjam - April 16, 2011

•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

8. Gurpreet Singh - November 13, 2008

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.

yagnesh patel - December 3, 2012

i am a computer engineer..your description is very easy to have some knowledge..i am giving exam for java subject..please give me some information about difference between method overloading and method overriding..thank you..

9. pramodh - November 26, 2008

hi,

could you please tell me in detail about final, finally, finalize.

Thanks

10. Difference B/w Finalize and Finally in .Net? - CrazyEngineers Forum - January 14, 2009

[...] Difference B/w Finalize and Finally in .Net? Difference between final, finally and finalize() ALLU __________________ Honor and [...]

11. Alexwebmaster - March 3, 2009

Hello webmaster
I would like to share with you a link to your site
write me here preonrelt@mail.ru

12. Prema - June 23, 2009

sir
could u say the exact meaning of final finally and finalize() method…which even the beginners can understand

13. Sanjeev - July 16, 2009

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.

Parul Vaidya - October 17, 2010

thanx…nice example to make concept clear

Bibhuti - December 17, 2010

thnx Nice Example

baskar from chennai - June 28, 2011

super practical example,please do the same for all java concepts…..by your one of the unknown fan:-)

gdsdfd - January 16, 2012

hilarious way to express ur thought man….GUD ONE …now got that!!!!

ajith - September 15, 2012

really great way of explaining

14. sanchit - October 6, 2009

very nice sanjeev… :)

15. AmitP - November 30, 2009

Nice One Sanjeev

16. nunnu - December 21, 2009

sanjeev, dont get emotional!

17. nishith - December 29, 2009

Final is keyword use to prevent further inheritance of any class :?

18. nishith - December 29, 2009

or in Sanjeev’s term final is key word to castrate the dog to stop further inheritance of character of that dog’s clan [ class ]

19. swetha - January 7, 2010

static and final both are constants , when we use static and whn we use final?

vivek - September 6, 2011

Hi ,

Let me clarify you that STATIC and FINAL are not both one at the same.

STATIC is a class level variable (which is not associated to any of the objects of a class)
Only one variable is maintained across all the objects of the class.

For example for evry student ,u maintain name,address,marks etc…. all these as object level varibles.
and when it is coming to the number of students in class make it as class varible (i.e., static)
Final is much same as constant in other languages .
Whose value will not get chaged at any point of time.

hope this will clarify you…

all the best….

Smita - September 11, 2011

Really nice info……………..

20. Surya - May 12, 2010

hi swetha

Static variables are initialized when the Class Loader loads the class and hence can be access in the static block declaration of the class without declaring an object. Whereas the final variable is only accessible after the declaration of an instance of an object.

fromchennaicity@gmail.com
Surya

21. bhawana - July 14, 2010

hello

radha krishan - September 20, 2010

hallo bhawana garu what r udng pressent this radha

indrayanee - April 13, 2012

hi

22. Punit - February 28, 2011

hello, can anyone tell me what is an abstract class??

hi :) - June 29, 2012

Abstract class is a class that can only be inherited. An abstract class can not have any base class. ie, it can not be inherited from a class.

23. N.kasthuraiah - March 4, 2011

I want examples for final,finally and finalize

24. prakaash kumar - May 25, 2011

what is object serilisation

25. abhishek - September 23, 2011

thanks .. really very very thank you

26. SABARI.C.G - October 3, 2011

thanks dudes

27. Answers to 15 Must Know Java Interview Questions After 2 Years of Experience | ExceptionHandle.com - November 22, 2011

[...] finalize() is an method that is invoked before an object is discarded by the garbage collector. Source | Final Usage |Finally [...]

28. visweswaraiah - March 15, 2012

nice sanjeev

29. padobado - March 22, 2012

Thanks, it helps

30. Вопросы Java собеседования, ответы на которые вы обязаны знать | IT Собеседования - April 20, 2012

[...] Source | Final Usage |Finally Usage | Finalize() [...]

31. susheel baseer - May 4, 2012

tell me full difference between final and finally in java

32. Anthony,Mccray,anthonymccra410,Identi - June 4, 2012

Nature knows best, and she says, roar!…

Just as courage imperils life, fear protects it….

33. What is the difference between final, finally and finalize? - All Tech In One - September 25, 2012

[...] difference posted by sravan.vinjam as comment on the website http://allu.wordpress.com/2006/11/08/difference-between-final-finally-and-finalize/ which looked [...]

34. Manjunathan - October 2, 2012

what is the difference between finalize and finalizer ?

35. tera baap - November 17, 2012

haram khor salo kutta kamina MC,BC,LKB………etc

36. Mayank goyal - April 11, 2013

Final- it is a keyword and also a class where we can’t override its method in another class
Finally-finally is used for execute mandatory statement inside exception handling. in the program exception occurred or no, finally always executes except system.exit() statement.with try we can use finally no need of catch.
Finalize- it is a application program which executes under jvm. it is used for garbage collection means to remove unnecessary data by scan the memory

37. computer repairs in hyderabad, - May 21, 2013

It’s a shame you don’t have a donate button! I’d without a doubt donate to this brilliant blog! I guess for now i’ll settle
for bookmarking and adding your RSS feed to my
Google account. I look forward to new updates and will share this blog with my Facebook group.
Talk soon!

38. phenixoption - July 20, 2013

Hello, Neat post. There is a problem together
with your site in web explorer, may check this?
IE still is the market leader and a good component
to other folks will leave out your wonderful writing
because of this problem.

39. How To Look Attractive - July 31, 2013

And fat is under the skin as the fatty layer between the muscles.
How well does the memory foam with your bed disperse warmth.
The most common place that people ground the amp is by
running a wire to one of the bolts that holds down the front
seat.

general - September 25, 2013

nice arguments

40. hunter - September 25, 2013

hello
how to be attractive can u give suggestion

41. mmorpg - March 23, 2014

I enjoy, result in I found just what I was taking a look for.
You’ve ended my 4 day lengthy hunt! God Bless you man.
Have a great day. Bye

42. happyheartedkitchen.blogspot.nl - August 22, 2014

Hmm is anyone else experiencing problems with the images on this blog loading?
I’m trying to figure out if its a problem on my end or if it’s the blog.
Any suggestions would be greatly appreciated.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: