final finally finalize in java

final finally finalize are keywords in Java, These keywords are used in exception handling.

final finally finalize in java

final in Java

final is the modifier in Java applicable for classes, methods, and variables. If a class is declared as final then we can’t extend that class in other words we can’t create a child class for the final class i.e., inheritance is not possible with the final class.

If a method is final then we can not override that method.

If a variable declares as final then we can not update the value of a variable.

finally in Java

finally is a block always associate with try catch to maintain clean up code.

try{
 // Risky code
}
catch(Exception e)
{
//Handling exception
}
finally
{
  // cleanup code
}

Irrespective of whether an exception is raised or not, whether an exception is handled or not finally block will be executed every time.

finalize in Java

finalize is a method always invoked by a garbage collector just before destroying an object to perform the cleanup activity. Once finalize method is completed, imideatiley garbage collector destroys that object.

Note-

finally, the block is responsible to perform cleanup activities related to the try-catch block, whereas finalize method is responsible to perform cleanup activities related to the object.

Similar Java Tutorials

6 thoughts on “final finally finalize in java”

Leave a Comment