|
| 1 | +--- |
| 2 | +title: Java final keyword |
| 3 | +description: In this tutorial, we will learn how to use the Java `final` keyword. |
| 4 | +--- |
| 5 | + |
| 6 | +In Java, the `final` keyword is used to define constants and can be applied to variables, methods, and classes. Declaring an entity as `final` ensures it is only assigned once, meaning: |
| 7 | + |
| 8 | +- A `final` variable cannot be reassigned. |
| 9 | +- A `final` method cannot be overridden. |
| 10 | +- A `final` class cannot be subclassed. |
| 11 | + |
| 12 | +## 1. final Variable in Java |
| 13 | + |
| 14 | +A `final` variable cannot have its value changed once it has been assigned. For instance: |
| 15 | + |
| 16 | +```java |
| 17 | +class Main { |
| 18 | + public static void main(String[] args) { |
| 19 | + |
| 20 | + // create a final variable |
| 21 | + final int AGE = 32; |
| 22 | + |
| 23 | + // attempt to change the final variable |
| 24 | + AGE = 45; |
| 25 | + System.out.println("Age: " + AGE); |
| 26 | + } |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +In this example, the variable `AGE` is marked `final`, meaning its value cannot be changed after its initial assignment. Attempting to reassign it will result in a compilation error: |
| 31 | + |
| 32 | +```plaintext |
| 33 | +cannot assign a value to final variable AGE |
| 34 | + AGE = 45; |
| 35 | + ^ |
| 36 | +``` |
| 37 | + |
| 38 | +> **Note:** By convention, `final` variables in Java are typically written in uppercase. |
| 39 | +
|
| 40 | +## 2. final Method in Java |
| 41 | +A `final` method cannot be overridden by subclasses. For example: |
| 42 | + |
| 43 | +```java |
| 44 | +class FinalDemo { |
| 45 | + // define a final method |
| 46 | + public final void display() { |
| 47 | + System.out.println("This is a final method."); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +class Main extends FinalDemo { |
| 52 | + // attempt to override the final method |
| 53 | + public final void display() { |
| 54 | + System.out.println("The final method is overridden."); |
| 55 | + } |
| 56 | + |
| 57 | + public static void main(String[] args) { |
| 58 | + Main obj = new Main(); |
| 59 | + obj.display(); |
| 60 | + } |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +Here, the `display()` method in the `FinalDemo` class is `final`, so it cannot be overridden in the `Main` class. Attempting to do so will generate a compilation error: |
| 65 | + |
| 66 | +```plaintext |
| 67 | +display() in Main cannot override display() in FinalDemo |
| 68 | + public final void display() { |
| 69 | + ^ |
| 70 | + overridden method is final |
| 71 | +``` |
| 72 | + |
| 73 | +## 3. final Class in Java |
| 74 | +A `final` class cannot be extended by any other class. For example: |
| 75 | + |
| 76 | +```java |
| 77 | +// define a final class |
| 78 | +final class FinalClass { |
| 79 | + public void display() { |
| 80 | + System.out.println("This is a final method."); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +// attempt to extend the final class |
| 85 | +class Main extends FinalClass { |
| 86 | + public void display() { |
| 87 | + System.out.println("The final method is overridden."); |
| 88 | + } |
| 89 | + |
| 90 | + public static void main(String[] args) { |
| 91 | + Main obj = new Main(); |
| 92 | + obj.display(); |
| 93 | + } |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +In this example, `FinalClass` is declared `final`, so it cannot be subclassed by `Main`. Attempting to inherit from it will result in a compilation error: |
| 98 | + |
| 99 | +```plaintext |
| 100 | +cannot inherit from final FinalClass |
| 101 | +class Main extends FinalClass { |
| 102 | + ^ |
| 103 | +``` |
0 commit comments