SrcForge

Lesson 28

Final Keyword

The final keyword restricts modification of variables, methods and classes. Final variables cannot be reassigned, final methods cannot be overridden and final classes cannot be inherited.

Java Track

Save progress as you learn.

61 lessons

Lesson content

The final keyword in Java is used to restrict modification of variables, methods and classes. It helps developers create constants, prevent method overriding and stop inheritance when needed.

What is the Final Keyword in Java?

The final keyword can be used with variables, methods and classes.

Definition List

  • Final Keyword: A Java keyword used to restrict modification of variables, methods and classes.
  • Final Variable: A variable whose value cannot be changed after it has been assigned.
  • Final Method: A method that cannot be overridden by a subclass.
  • Final Class: A class that cannot be inherited by another class.

Final Variable in Java

A final variable can only be assigned a value once. After assignment, its value cannot be modified.

Declaration

final datatype var_name;

A final variable can be initialized directly during declaration or inside a constructor.

Example 1: Final Variable

class Test {
    final int MIN = 1;
    final int NORMAL;
    final int MAX;

    Test(int normal) {
        NORMAL = normal;
        MAX = 100;
    }

    void display() {
        System.out.println("MIN    : " + MIN);
        System.out.println("NORMAL : " + NORMAL);
        System.out.println("MAX    : " + MAX);
    }
}

public class finalTest {
    public static void main(String args[]) {
        Test o = new Test(50);
        o.display();
    }
}

Key Points About Final Variables

  • A final variable must be assigned exactly once.
  • Its value cannot be reassigned.
  • Can be initialized during declaration or in a constructor.
  • Often used for constants.

Final Method in Java

When a method is declared as final, it cannot be overridden by any subclass.

Declaration

final void methodName() {
    // body
}

Example 2: Final Method

class Super {
    public void display() {
        System.out.println("I am Super Display");
    }

    final void finalDisplay() {
        System.out.println("I am Super Final Display");
    }
}

class sub extends Super {
    public void display() {
        System.out.println("I am Sub Display");
    }
}

public class finalMethods {
    public static void main(String args[]) {
        sub o = new sub();
        o.display();
        o.finalDisplay();
    }
}

Why Use Final Methods?

  • Prevents method overriding.
  • Protects critical business logic.
  • Improves security and consistency.
  • Ensures subclasses cannot alter important functionality.

Final Class in Java

A class declared with the final keyword cannot be extended by another class.

Declaration

final class ClassName {
    // body
}

Example 3: Final Class

final class finalClassDemo {
    public void display() {
        System.out.println("I am Display");
    }
}

public class finalClasss {
    public static void main(String[] args) {
        finalClassDemo o = new finalClassDemo();
        o.display();
    }
}

Why Use Final Classes?

  • Prevents inheritance.
  • Protects class behavior from modification.
  • Useful for creating immutable and secure classes.
  • Commonly used in Java API design.

Differences: Final Variable, Method and Class

  • Final Variable: Cannot be reassigned.
  • Final Method: Cannot be overridden.
  • Final Class: Cannot be extended.

Advantages of Using the Final Keyword

  • Improved Security: Prevents unwanted changes to important code.
  • Better Code Stability: Ensures variables, methods and classes behave as intended.
  • Easier Maintenance: Makes program behavior more predictable.
  • Supports Immutable Objects: Final variables help create immutable classes.

Best Practices

  • Use final for constants.
  • Mark methods as final when overriding should not be allowed.
  • Use final classes when inheritance is unnecessary.
  • Apply final to variables that should never change.

FAQ

1. What is the purpose of the final keyword in Java?

The final keyword is used to restrict modification of variables, methods and classes, making code more secure and predictable.

2. Can a final variable be initialized inside a constructor?

Yes. A final variable can be assigned a value inside a constructor, but only once.

3. Can a final class be inherited in Java?

No. A final class cannot be extended or inherited by another class.

final keyword restricts modification in Java.
Final variables cannot be reassigned after assignment.
Final methods cannot be overridden by subclasses.
Final classes cannot be inherited.
Final variables can be initialized in a constructor.
Improves security, stability and maintainability.
Commonly used to create constants.

Where Java is used

Java in real-world development

Final Variable

Assigned only once. Value cannot be changed. Used for constants.

Final Method

Cannot be overridden by any subclass. Protects critical logic.

Final Class

Cannot be extended. Prevents inheritance entirely.

Constructor Init

A final variable can be initialized inside a constructor, but only once.

Security

final prevents unwanted modification to variables, methods and classes.

Immutability

Final variables are key to building immutable objects in Java.