Final keyword in Java

In the world of Java programming, there are certain keywords that play significant roles in defining the behavior and characteristics of classes, methods, and variables. One such keyword is ‘final,’ which can be applied to variables, methods, and classes. Lets explore the Java ‘final’ keyword, explore its purpose, and discuss some important points you should be aware of. Additionally, we will provide practical examples to help you grasp its usage more effectively.

  1. Final Variables:
    When the ‘final’ keyword is applied to a variable, it means that the variable’s value cannot be changed once assigned.
    • Here are a few key points to remember:
      • Initialization: A final variable must be initialized at the time of declaration or within the constructor. Once assigned, the value cannot be modified.
      • Benefits: Final variables offer benefits such as code readability, preventing accidental modifications, and enabling compiler optimizations.
      • Naming conventions: By convention, final variables are usually named in uppercase with underscores to separate words.

Example:

Java
public class Circle {
    private final double PI = 3.14159;
    private final int radius;

    public Circle(int radius) {
        this.radius = radius;
    }

    public double calculateArea() {
        return PI * radius * radius;
    }
}
  1. Final Methods:
    When a method is declared as ‘final,’ it means that it cannot be overridden by subclasses.
    • Here are the key points:
      • Inheritance restriction: By making a method final, you prevent any subclass from changing its implementation. This is useful when you want to ensure the consistency of behavior across different class hierarchies.
      • Performance benefits: The final keyword allows the compiler to optimize method calls, as it knows that the method cannot be overridden.

Example:

Java
public class Shape {
    public final void draw() {
        // Drawing logic
    }
}

public class Circle extends Shape {
    // Cannot override the 'draw' method
}
  1. Final Classes:
    When a class is declared as ‘final,’ it cannot be extended, meaning you cannot create subclasses of a final class.
    • Here’s what you should know:
      • Design intent: Final classes are typically used when the class design is complete, and no further extension is desired. They ensure that the class behavior remains consistent.
      • Performance optimizations: Similar to final methods, final classes allow the compiler to perform certain optimizations, making method calls more efficient.

Example:

Java
public final class MathUtils {
    public static int add(int a, int b) {
        return a + b;
    }
}

// Cannot extend the 'MathUtils' class

Must-Know Points about the ‘final’ Keyword:

  1. Blank final variables: A blank final variable is declared as ‘final’ but not assigned a value during declaration. It must be assigned within the constructor.
  2. Final parameters: The ‘final’ keyword can be applied to method parameters, indicating that the parameter cannot be modified within the method.
  3. Final and immutability: Although ‘final’ promotes immutability, it is important to note that it does not make objects immutable. It only ensures that the reference to the object cannot be changed.
  4. Interfaces and ‘final’: Interface methods are implicitly ‘final’ and cannot be overridden. However, the ‘final’ keyword cannot be explicitly applied to interface methods.

The ‘final’ keyword in Java plays a crucial role in defining immutability, prohibiting method overriding, and restricting class extension. By understanding its purpose and appropriate usage, you can enhance the robustness and maintainability of your Java code. Remember the key points discussed in this blog post and leverage practical examples to apply the ‘final’ keyword effectively in your own projects.

Leave a Comment

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

Scroll to Top