Java final keyword

    final keyword

    In Java, the final is a keyword that allows restricting the user. It means the data members which are defined as final cannot be changed by the user. This ‘final’ keyword is used in the context of the variable, method and class.

    If a variable is final and has no value it is called the blank final variable which can be initialized only in the constructor. this black final variable can also be static which can be initialized within the static block.

    Final variable

    In Java, the final means that the value cannot be changed. If the variable is declared as final then you cannot change its value and it will act as a constant.

    Example-

    class Demo{  
    
     final int speed=90;  
    
     void run(){  
    
      speed=400;  
    
     }  
    
     public static void main(String args[]){  
    
     Demo obj=new  Demo();  
    
     obj.run();  
    
     }  
    
    }

    Output-

    Compile-time error

    In the above example, speed is the final variable and we are trying to change its value in the run method. Thus it will give a compile-time error.

    Final method

    Once you declare the method as final, you cannot override that method in its subclass. If overridden then you will get a compile-time error.

    Example-

    class Demo{  
    
      final void run(){System.out.println("running");}  
    
    }  
    
         
    
    class Big extends Demo{  
    
       void run(){System.out.println("running safely");}  
    
         
    
       public static void main(String args[]){  
    
       Big b= new Big();  
    
       b.run();  
    
       }  
    
    }  

    Output-

    Compile-time error

    Final class

    If you declare any class with the final keyword then you cannot extend the class and access its data members.

    Example-

    final class Demo{}  
    
    class Bike extends Demo{  
    
      void run(){System.out.println("running");}  
    
      public static void main(String args[]){  
    
      Bike b= new Bike();  
    
      b.run();  
    
      }  
    
    }  

    Output-

    Compile-time error

    Note :

    • You can extend the method which is final but you cannot override it.

    Example-

    class Demo{  
    
      final void run(){System.out.println("Run");}  
    
    }  
    
    class Bike extends Demo{  
    
       public static void main(String args[]){  
    
        new Bike().run();  
    
       }  
    
    }  

    Output-

    Run

    • You can initialize the blank final variable in the constructor.

    Example-

    class Demo{  
    
      final int speed;
    
        
    
      Demo(){  
    
      speed=70;  
    
      System.out.println(speed);  
    
      }  
    
      
    
      public static void main(String args[]){  
    
        new Demo();  
    
     }  
    
    }  

    Output-

    70

    • If you do not initialize the static and final variable at the time of declaration then it is called the static blank final variable. You can initialize the value in the static block only.

    Example-

    class Demo{  
    
      static final int data;
    
      static{ data=50;}  
    
      public static void main(String args[]){  
    
        System.out.println(Demo.data);  
    
     }  
    
    }  

    Output-

    50
    • You cannot declare the constructor as final.