Difference between final, finally, and finalize

final :

In Java, "final" is a keyword used to declare a variable, method, or class as a constant or unchangeable. Once a variable is declared as "final," its value cannot be modified. If a method is marked as "final," it cannot be overridden in any subclass. Similarly, if a class is declared as "final," it cannot be subclassed.

final variable: When used with variables, the "final" keyword makes the variable a constant with an unchangeable value throughout the program. It must be initialized at the time of declaration and cannot be modified thereafter.

final variable example:

final int constantValue = 10;
final method :  When used with methods, the "final" keyword prevents the method from being changed or modified in any subclass, ensuring consistent behavior across all subclasses.

final method example:
class Parent {
    final void someMethod() {
        // Method implementation
    }
}

class Child extends Parent {
    // It's not allowed to override the final method someMethod()
}
final class : When applied to classes, the "final" keyword prevents the class from being subclassed. It ensures that the class cannot be extended by other classes, and thus its functionality remains unchanged throughout the program.

final class example:
final class FinalClass {
    // Class implementation
}

// It's not allowed to extend the final class FinalClass
finally :
In Java, "finally" is a block associated with a try-catch block and is used to ensure that certain code executes regardless of whether an exception is thrown or not. The code inside the "finally" block will always be executed, no matter if an exception occurs or not.

finally block syntax:
try {
    // Code that may throw an exception
} catch (Exception e) {
    // Exception handling code
} finally {
    // Code that will always be executed
}
finalize:
"finalize" is not a keyword in Java; instead, it is a method defined in the Object class. It is invoked by the garbage collector before reclaiming memory occupied by an object that is no longer reachable and eligible for garbage collection. Nevertheless, it is generally discouraged in modern Java programming due to its drawbacks, such as unpredictable execution timing and the potential for resource leaks.

finalize code example:
class MyClass {
    @Override
    protected void finalize() throws Throwable {
        // Finalization code
        super.finalize();
    }
}
Difference between final, finally, and finalize:

final: Used as a modifier for variables, methods, or classes to make them unchangeable, un-overridable, or un-extendable, respectively.

finally: A block associated with try-catch that ensures code inside it always executes, regardless of whether an exception is thrown or not.

finalize: A method in the Object class, called by the garbage collector before reclaiming memory; discouraged in modern Java due to unpredictable execution and resource leak issues.
Next Post Previous Post
No Comment
Add Comment
comment url