English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Array

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Object-Oriented (III)

Java Exception Handling

Java List (List)

Java Queue (Queue)

Java Map Collection

Java Set Collection

Java Input/Output (I)/O)

Java Reader/Writer

Java Other Topics

Java final Keyword

In this tutorial, we will learn about Java final variables, final methods, and final classes through examples.

In Java, the final keyword is used to represent constants. It can be used with variables, methods, and classes.

Any entity (variable, method, or class) once declared as final can only be assigned once. That is,

  • final variable cannot be reinitialized with another value

  • final method cannot be overridden

  • final class cannot be inherited

1. Java final variable

In Java, we cannot change the value of a final variable. For example,

class Main {
  public static void main(String[] args) {
    //Create a final variable
    final int AGE = 32;
    //Attempt to change a final variable
    AGE = 45;
    System.out.println("Age: " + AGE);
  }
}

In the above program, we created a final variable named age and tried to change the value of the final variable.

When running the program, the following error message will appear, indicating a compilation error.

cannot assign a value to final variable AGE
    AGE = 45;
    ^

Note:It is recommended to use uppercase letters to declare final variables in Java.

2. Java final method

Before understanding final methods and final classes, make sure you understandJava Inheritance.

In Java, this final method cannot be overridden by subclasses. For example,

class FinalDemo {
    //Create final method
    public final void display() {
      System.out.println("This is the Final method.");
    }
}
class Main extends FinalDemo {
  //Attempt to override final method
  public final void display() {
    System.out.println("Final method is overridden.");
  }
  public static void main(String[] args) {
    Main obj = new Main();
    obj.display();
  }
}

In the above example, we created a final method named display() within the FinalDemo class. Here, the Main class inherits from the FinalDemo class.

We tried to override the final method in the Main class. When running the program, the following error message will appear, indicating a compilation error.

 display() in Main cannot override display() in FinalDemo
  public final void display() {
                    ^
  overridden method is final

3. Java final class

In Java, a final class cannot be inherited by another class. For example,

final class FinalClass {}}
    //Create final method
    public void display() {
      System.out.println("This is a final method.");
    }
}
class Main extends FinalClass {
  //Attempt to override final method
  public void display() {
    System.out.println("Override final method");
  }
  public static void main(String[] args) {
    Main obj = new Main();
    obj.display();
  }
}

In the above example, we created a final class named FinalClass. Here, we attempt to inherit from the final class through the Main class.

When running the program, the following error message will appear, indicating a compilation error.

cannot inherit from final FinalClass
class Main extends FinalClass {
                   ^