Final vs Finally vs Finalize in Java

In Java, final, finally, and finalize are three different keywords that are used in different contexts. Here's a detailed explanation of each keyword:

Final:

The final keyword is used to declare a variable, method, or class as immutable or unchangeable. If a variable is declared as final, its value cannot be modified once it is initialized. If a method is declared as final, it cannot be overridden by a subclass. If a class is declared as final, it cannot be subclassed. Here's an example of how to use the final keyword in Java:

                
    public class Example {
        public static final int MAX_VALUE = 100;
        public final void doSomething() {
            // code that cannot be overridden
        }
        public static final class MyFinalClass {
            // code for a final class
        }
    }
                
            

In this example, we have declared a MAX_VALUE variable as final, which means its value cannot be modified once it is initialized. We have also declared a doSomething() method as final, which means it cannot be overridden by a subclass. Finally, we have declared a MyFinalClass as final, which means it cannot be subclassed.

Finally

The finally keyword is used in a try-catch block to specify a block of code that should be executed regardless of whether an exception is thrown or not. The finally block is always executed after the try block or the catch block, regardless of whether an exception is thrown or not. Here's an example of how to use the finally keyword in Java:

                
    public class Example {
        public static void main(String[] args) {
            try {
                // code that may throw an exception
            } catch (Exception e) {
                // code to handle the exception
            } finally {
                // code to be executed regardless of whether an exception is thrown or not
            }
        }
    }
                
            

In this example, we have a try-catch block that may throw an exception. The catch block is used to handle the exception if it is thrown. The finally block is used to specify a block of code that should be executed regardless of whether an exception is thrown or not.

Finalize:

The finalize keyword is used to specify a method that should be called by the garbage collector before an object is garbage collected. The finalize method is called when an object is no longer reachable or referenced by any other object in the program. Here's an example of how to use the finalize keyword in Java:

                
    public class Example {
        @Override
        protected void finalize() throws Throwable {
            // code to be executed before the object is garbage collected
        }
    }
                
            

In this example, we have overridden the finalize method to specify a block of code that should be executed before the object is garbage collected. The finalize method is called automatically by the garbage collector, and we do not need to call it explicitly.

In summary, final, finally, and finalize are three different keywords in Java that are used in different contexts. final is used to declare a variable, method, or class as immutable or unchangeable. finally is used in a try-catch block to specify a block of code that should be executed regardless of whether an exception is thrown or not. finalize is used to specify a method that should be called by the garbage collector before an object is garbage collected.