Home >>Java Tutorial >Final Keyword in Java

Final Keyword in Java

Final Keyword in Java

The final keyword in Java is basically a non-access modifier that is used for classes, attributes and methods that usually makes them non-changeable in simpler words it is impossible to inherit or override. The final keyword is mainly useful whenever the programmer or the user wants a variable to always store the exact same value, for instance, PI (3.14159...). If we have to define the final keyword in one word then it will be a "modifier".

Talking about the usage of the final keyword, it can generally be applied with the variables. A final variable is that variable that basically has no value and it is known as blank final variable or in other words, uninitialized final variable. This can be initialized only in the constructor. The blank final variable can also be static that will be initialized only in the static block.

Here are the basics of the final variable in Java that basically consists of the three types that are depicted below:

  • Java Final Variable
  • Java Final methods
  • Java Final Classes

1. Java Final Variable

A final variable in Java can generally be explicitly initialized only once. A reference variable that has been declared final can never be reassigned in order to refer to a different object. On the other hand, the data that is within the object can be changed obviously. Hence, Programmers can only change the state of the object but not the reference. The final modifier is often used with the static in order to make the constant a class variable and that is only possible with the variables.

Here is an example of the Java Final Variable that will make you understands the concept of it very clearly and will help you in understanding the core concept of it:

public class Demo 
{
   final int val = 10;

   //Declaring constants
   public static final int BOXSIZE = 10;
   static final String TITLE = "Developer";

   public void changeValue() 
   {
      val = 12; // will give an error
   }
}
Output:
Test.java:9: error: cannot assign a value to final variable value

2. Java Final Methods

A final method in Java usually cannot be overridden by any of the subclasses. As we have already mentioned above that the final modifier blocks a method from being modified in a subclass. The main reasons of making a method final is to aim at the result that content of the method should not be changed by any outsider at any cost.

Here is an example of the Java Final Method that will help you understand the physical aspect of it and it will help you in grasping the concept from the very depth of it:

public class Test 
{
   public final void changeName() 
   {
      // body of method
   }
}

3. Java Final Classes

The main reason that is responsible for the use of the Java Final Classes is being declared as the final is only to prevent the class from being subclassed. In case, a class is found to be marked as final then none of the class can inherit any feature from the final class. In case, the programmer makes any class as final then they will not be able to extend it.

Here is an example of the Java Final Classes that will give you the insight of the physical aspect of it and will make you understand its use in the Java language:

final class Bike{}  
  
class Hero extends Bike
{  
  void run()
  {
	System.out.println("running safely with 100kmph");
  }  
    
  public static void main(String args[])
  {  
	Hero hero= new Hero();  
	hero.run();  
  }  
}  
Output:
Bike.java:3: error: cannot inherit from final Bike

No Sidebar ads