final keyword in Java

What is the final keyword in Java

In Java, the final keyword is a modifier that can be applied to variables, methods, and classes. It indicates that the entity to which it is applied cannot be changed or overridden, providing various levels of immutability, security, and control in your Java programs.

1. Final Variables:

When a variable is declared as final, it means its value cannot be changed once assigned. This is often used for constants or variables that should not be modified.

Example:

Final Variable Example
/*
 * Author: Zameer Ali Mohil
 * */
public class FinalVariableExample {
    public static void main(String[] args) {

        final int MAX_VALUE = 100; 
        // MAX_VALUE = 200; // Attempting to change the value will result in a compilation error
        System.out.println("Maximum Value: " + MAX_VALUE);
    }
}

In this example, MAX_VALUE is declared as a final variable, and attempting to change its value will result in a compilation error.

2. Final Methods:

When a method is declared as final in a class, it means the method cannot be overridden by subclasses. This is useful when you want to enforce a specific behavior and prevent subclasses from changing it.

Example:

Final Method Example
/*
 * Author: Zameer Ali Mohil
 * */
class Parent {
    final void display() { // Final method
        System.out.println("This is a final method in the Parent class.");
    }
}

class Child extends Parent {
    // Attempting to override the final method will result in a compilation error
    /*void display() {
        System.out.println("Attempting to override the final method in the Child class.");
    }*/
}

In this example, the display() method in the Parent class is declared as final. Attempting to override this method in the Child class will result in a compilation error.

3. Final Classes:

When a class is declared as final, it means the class cannot be subclassed. This is often used for classes that should not have any subclasses, ensuring that the class’s behavior and structure remain unchanged.

Example:

Final Class Example
/*
 * Author: Zameer Ali Mohil
 * */
final class FinalClass {
    // Class members and methods
}

// Attempting to create a subclass of FinalClass will result in a compilation error
/*class SubClass extends FinalClass {
    // Class members and methods
}*/

In this example, the FinalClass is declared as final, preventing any subclass from being created. Attempting to create a subclass of FinalClass will result in a compilation error.

4. Final Parameters:

When a method parameter is declared as final, it means the parameter cannot be modified within the method. This is useful to ensure that the parameter’s value remains constant during the method execution.

Example:

Final Parameter Example
/*
 * Author: Zameer Ali Mohil
 * */
public class FinalParameterExample {
    void process(final int value) {
        // The value of 'value' cannot be changed within this method
        // value = 20; // Attempting to change the value will result in a compilation error
        System.out.println("Processed Value: " + value);
    }

    public static void main(String[] args) {
        FinalParameterExample example = new FinalParameterExample();
        example.process(10);
    }
}

In this example, the process() method takes a final parameter value. Attempting to modify the value of this parameter within the method will result in a compilation error.

These examples illustrate how the final keyword can be used for variables, methods, and classes, enforcing immutability, preventing method overrides, and prohibiting class inheritance, respectively.

Leave a Reply

Your email address will not be published. Required fields are marked *