Tuesday, 17 November 2015

What is difference between final, finally and finalize() in Java

final and finally are keywords in java whereas finalize is a method.
final keyword: We can use final keyword in 3 ways as shown below:
final variable: It is a constant we can't change the value.
final method: we can't override the methods
final class: We can't create child classes
finalize(): is used to identify an object is eligible for Garbage Collection or not. GC calls this method just before destroying an object.
finally: Finally block is used for clean up code. Which will always execute(including return statement) whether exception is raised or not but except below 3 conditions.
    a) system.exit(0)
    b) out of memory (there is no space for executing finally block)
    c) stack overflow error (infinite loop)
Note: The main purpose of finalize() method and finally block is cleaning the resources but finally meant for clean up activities related to try block where as finalize() meant for clean up activities related to Object.

No comments:

Post a Comment