Final Keyword in Java

Learn how to use the final keyword with variables, methods, and classes in Java. Explore rules, examples, and FAQs to understand its importance.


What is final in Java?

In Java, final is a keyword used to restrict the user. It can be applied to variables, methods, and classes.

  • Final Variable: Value cannot be changed (acts as a constant).
  • Final Method: Cannot be overridden by child classes.
  • Final Class: Cannot be extended (no inheritance).

Rules for Final Variables in Java

FinalVariableInitializationExample.java Copy Code
public class FinalVariableInitializationExample {

    // Final instance variable initialized at the time of declaration
    final int x = 10;

    // Blank final instance variable, must be initialized in constructor
    final int y;

    // Constructor to initialize the blank final variable
    public FinalVariableInitializationExample() {
        y = 20; // Initialization of final variable 'y'
    }

    public static void main(String[] args) {
        // Create an object of the class
        FinalVariableInitializationExample obj = new FinalVariableInitializationExample();

        // Access and print final variables
        System.out.println("x: " + obj.x); // Output: 10
        System.out.println("y: " + obj.y); // Output: 20
    }
}
    
FinalVariableReassignmentExample.java Copy Code
public class FinalVariableReassignmentExample {
    final int x = 10;

    public void modifyFinalVariable() {
        // x = 20; // error: cannot assign a value to final variable
    }

    public static void main(String[] args) {
        System.out.println("x: " + new FinalVariableReassignmentExample().x);
    }
}   
FinalParameterExample.java Copy Code
public class FinalParameterExample {
    public void printValue(final int x) {
        System.out.println("x: " + x);
        // x = 20; // error: cannot assign a value to final parameter
    }

    public static void main(String[] args) {
        new FinalParameterExample().printValue(10);
    }
}   
FinalMethodExample.java Copy Code
class Parent {
    public final void show() {
        System.out.println("This is a final method.");
    }
}
class Child extends Parent {
    // public void show() { } // error: cannot override final method
}
public class FinalMethodExample {
    public static void main(String[] args) {
        new Child().show();
    }
}   
FinalClassExample.java Copy Code
final class Vehicle {
    void display() {
        System.out.println("This is a final class.");
    }
}
// class Car extends Vehicle { } // error: cannot inherit from final class
public class FinalClassExample {
    public static void main(String[] args) {
        new Vehicle().display();
    }
}   
AnonymousClassExample.java Copy Code
public class AnonymousClassExample {
    public void printValue() {
        final int x = 10; // effectively final
        Runnable r = new Runnable() {
            @Override
            public void run() {
                System.out.println("Value of x: " + x);
                // x = 20; // Error: cannot assign a value to final variable
            }
        };
        r.run();
    }

    public static void main(String[] args) {
        new AnonymousClassExample().printValue();
    }
}   

Summary

The final keyword in Java is used to restrict the user. It can be applied to variables, methods, and classes. A final variable cannot be reassigned, a final method cannot be overridden, and a final class cannot be subclassed.

Frequently Asked Questions

Yes, final methods are inherited but cannot be overridden.

A final variable not initialized at declaration. It can be initialized only in the constructor.

A static final variable not initialized at declaration. It must be initialized in a static block.

Next: Java Input and Output



πŸš€ Quick Knowledge Check

Topic: Final-keyword | Language: Java

Q1. Which of the following is true about a final class in Java?
Q2. What does the `final` keyword do when applied to a variable in Java?
Q3. What is a blank final variable in Java?
Q4. What happens if a subclass tries to override a final method?
Q5. Why must variables accessed inside an anonymous class be final or effectively final?