Java final Keyword

The final keyword in Java is used to restrict the usage of variables, methods, and classes. Once a final variable is assigned a value, it cannot be changed. A final method cannot be overridden by subclasses, and a final class cannot be subclassed.

Table of Contents

  1. Introduction
  2. final Keyword Syntax
  3. Understanding final
  4. Examples
    • Final Variables
    • Final Methods
    • Final Classes
  5. Real-World Use Case
  6. Conclusion

Introduction

The final keyword is an important feature in Java that provides a way to make entities immutable. This can be applied to variables, methods, and classes to prevent them from being modified, overridden, or inherited, respectively.

final Keyword Syntax

Final Variable:

final dataType variableName = value;

Final Method:

final returnType methodName(parameters) {
    // method body
}

Final Class:

final class ClassName {
    // class body
}

Understanding final

Final Variables:

  • A final variable cannot be reassigned once it has been assigned a value.
  • final variables must be initialized when they are declared or in the constructor.

Final Methods:

  • A final method cannot be overridden by subclasses.
  • This is useful for methods that should have a fixed behavior in the superclass.

Final Classes:

  • A final class cannot be subclassed.
  • This is useful for creating immutable classes or utility classes that should not be extended.

Examples

Final Variables

A final variable can be either a constant or a variable that must be initialized once.

Example

public class FinalVariableExample {
    final int MAX_VALUE = 100;

    void display() {
        System.out.println("MAX_VALUE: " + MAX_VALUE);
    }

    public static void main(String[] args) {
        FinalVariableExample example = new FinalVariableExample();
        example.display();
    }
}

Output:

MAX_VALUE: 100

Final Methods

A final method cannot be overridden by subclasses.

Example

class Base {
    final void show() {
        System.out.println("Base class method.");
    }
}

class Derived extends Base {
    // Attempting to override final method will result in a compile-time error
    // void show() {
    //     System.out.println("Derived class method.");
    // }
}

public class Main {
    public static void main(String[] args) {
        Derived obj = new Derived();
        obj.show();
    }
}

Output:

Base class method.

Final Classes

A final class cannot be subclassed.

Example

final class FinalClass {
    void display() {
        System.out.println("This is a final class.");
    }
}

// Attempting to extend a final class will result in a compile-time error
// class DerivedClass extends FinalClass {
// }

public class Main {
    public static void main(String[] args) {
        FinalClass obj = new FinalClass();
        obj.display();
    }
}

Output:

This is a final class.

Real-World Use Case

Creating Immutable Classes

In real-world applications, the final keyword is often used to create immutable classes, where the state of an object cannot be changed once it is created. This is particularly useful for creating instances that are thread-safe and reliable.

Example

public final class ImmutableClass {
    private final String name;
    private final int age;

    public ImmutableClass(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }
}

public class Main {
    public static void main(String[] args) {
        ImmutableClass person = new ImmutableClass("Alice", 30);
        System.out.println("Name: " + person.getName());
        System.out.println("Age: " + person.getAge());
    }
}

Output:

Compilation failed.

Conclusion

The final keyword in Java is a powerful feature that provides a way to make variables, methods, and classes immutable. By understanding and using the final keyword, you can create more reliable and maintainable code. It helps prevent accidental modification of variables, ensures that methods have consistent behavior, and prevents classes from being improperly extended.

Leave a Comment

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

Scroll to Top