+ 14
How is final different from finally and finalize()?
5 odpowiedzi
+ 7
finalize() is a method of object that will execute when the object is garbage collected. It shouldn't generally be used since garbage collection cannot be determined and may not even happen during an application, so the circumstances for its use are quite specific and you need to know what you're doing
+ 5
final marks a variable as constant. It means that any attempt to chamge the variable's content will result in an error -,you can't change it's value.
finally is used in try/catch block. It contains statements that will be executed whether an exception has been thown and catched or not.
try{
,/* ... */
}catch(Exception e){ /* ... */ }
finally {System.out.println("Try /catch block exited");}
But I don't know what finalize() does.
+ 5
final is the keyword and we can make final variable, method and class ..if i make final variable it's means i can't change this value after initialization and.. if i make a final method it's means that i can't override it... And if i make a final class it's mean that i can't extend this class
finally is the block... If u know about exception then u should know about try catch finally ..we write risky code in try block example→ suppose if u write 8 lines of code and the exception will rise in line no.5 then after exception your code will jumped on catch () and rest line (6,7,8) of your code will not execute. So if u want to execute any code which is the important so you can write that's code in finally block ... Finally means it's 100% execute if exception occur OR not.
Finalize is the method which is the use for execute our final code before destroying our object by garbage collector as u knw that in java we always create object dynamically with new and there are no delete keyword...
+ 3
So garbage collector release object memory and if u want to perform final task before destroy our object then we call finalize method.its something like C++ Destructor.
hope u clear about final vs finally vs finalize
thankyou
+ 2
final is Access modifier which is use with Classes, method and variable.
If we declare a class as final then we can't extend that class.
If we declare method as final then we can't override that in child class.
If we declare a variable as a final then we can't change the value of that variable.
Finally is block which is always associated with the try catch block. The purpose of finally is code clean-up like close the database connection etc.
finalize() is a method which is present in Object class. finalize method always invoke by garbage collector before destroying the object. the purpose of finalize method is code clean-up related with object.