Home > DeveloperSection > Interviews > What is the difference between final, finally and finalize?

Posted on    May-07-2011 12:55 AM

 Java Java 
Ratings:
 1 Answer(s)
  3439  View(s)
Rate this:

Amit Singh

Total Post:565

Points:2825
Posted on    May-07-2011 12:00 AM

o final - declare constant
o finally - handles exception
o finalize - helps in garbage collection

Variables defined in an interface are implicitly final. A final class can’t be extended i.e., final class may not be subclassed. This is done for security reasons with basic classes like String and Integer. It also allows the compiler to make some optimizations, and makes thread safety a little easier to achieve. A final method can’t be overridden when its class is inherited. You can’t change value of a final variable (is a constant). finalize() method is used just before an object is destroyed and garbage collected. finally, a key word used in exception handling and will be executed whether or not an exception is thrown. For example, closing of open connections is done in the finally method.


Don't want to miss updates? Please click the below button!

Follow MindStick