final keyword in Java

Java final Keyword with Example

In this tutorial, we will learn about the final keyword in java:

  1. final variables,
  2. methods,
  3. classes

with example.

what is the final keyword in Java?

When declaring an entity in Java, the final keyword can be used. The use of the final keyword indicates that the value cannot be changed in the future. This entity can be a variable, a class, or a method, but it is not limited to these options.

Constants are denoted by the final keyword in Java. Variables, methods, and classes can all be used with it. Once a variable, method, or class has been declared final, it can only be assigned once. That is to say,

If you mark a variable as final, you won’t be able to change its value.
  • It is not possible to re-initialize the final variable with a different value.
  • It is not possible to override the final method.
  • It is impossible to extend the final class.

Java final Variable

The Java final keyword is a non-access specifier that is used to limit the scope of a class, variable, or method. If we use the final keyword to initialize a variable, we cannot change its value. If we declare a method to be final, no subclasses can override it.

class Sample{  
 final int limit= 100;//final variable  
 void run(){  
  limit=200;  
 }  
 public static void main(String args[]){  
 Sample obj= new  Sample();  
 obj.run();  
 }  
}

In the preceding program, we defined a final variable called run. And we attempted to alter the final variable’s value.

When we run the program, we will receive a compilation error with the message shown below.

Output:

Output: Compile Time Error

Java final Method

Some or all of a class’s methods can be declared final. In a method declaration, the final keyword indicates that the method cannot be overridden by subclasses.

class Sample {
    // create final method
    public final void display() {
      System.out.println("This is final method.");
    }
}

class Main extends Sample {
  // override final method
  public final void display() {
    System.out.println("The final method is overridden.");
  }

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

In the preceding example, we created a final method called display() within the Sample class. The Sample class is inherited by the Main class in this case.

In the Main class, we attempted to override the final method. When we run the program, we will receive a compilation error with the message shown below.

Output:

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

Java final Class

A final class is one that cannot be extended in any way. Methods can also be marked as final to indicate that they cannot be overridden by subclasses. Preventing the class from being subclassed could be especially useful if you write APIs or libraries and don’t want them to be extended to change the base behavior.

Note: The final class in Java cannot be inherited by another class.
final class Sample{}  
  
class Demo extends Sample{  
  void run(){
System.out.println("running safely with 120kmph speed");
}  
    
  public static void main(String args[]){  
  Demo S= new Demo();  
  S.run();  
  }  
}

In the preceding example, we created a final Sample Class. In this case, we attempted to have the Demo class inherit the Sample class.

When we run the program, we will receive a compilation error with the message shown below.

Output:

cannot inherit from final Sample
class Demo extends Sample {
^

You may like:

Java this Keyword with Example

Hope this article will guide you to recognize all about the Java final keyword with Examples that you needed and still if you have any problem or queries regarding this, post them in the comments section and we will be glad to assist you.

This Post Has 14 Comments

Leave a Reply