What is Final Keyword in Java In depth, all Doubt Question.

Hello Everyone. This is Gautam, and today's topic is Final Keyword in Java in easiest way, After reading this post you will have no doubt,

Final Keyword in Java

DefinitionIn the Java programming language, the final keyword is used in several contexts to define an entity that can only be assigned once. Once a final variable has been assigned, it always contains the same value. Final Can Be

  1. Final variable
  2. Final method
  3. Final class

The final keyword can be applied with the variables, a final variable that have no value it is called blank final variable or uninitialized final variable. It can be initialized in the constructor only. The blank final variable can be static also which will be initialized in the static block only. We will have detailed learning of these. Let's first learn the basics of final keyword.

1) Java final variable

If you make any variable as final, you cannot change the value of final variable(It will be constant).

Example of final variable

There is a final variable speedlimit, we are going to change the value of this variable, but It can't be changed because final variable once assigned a value can never be changed.

  1. class Duke
  2. {  
  3.    final int speedlimit=120;//final variable  
  4.    void run()
  5.   {  
  6.     speedlimit=300;  
  7.    }  
  8.  public static void main(String args[])
  9.  {  
  10.     Duke obj=new  Duke();  
  11.     obj.run();  
  12.   }  
  13. }//end of class  

Test it Now

Output: Compile Time Error


2. Java final method

If you make any method as final, you cannot override it.

Example of final method

  1. class car
  2. {  
  3.   final void run()
  4.   {
  5.      System.out.println("running");
  6.   }  
  7. }  
  8.      
  9. class Luxury extends car
  10. {  
  11.    void run()
  12.   {
  13.      System.out.println("running safely with 150 kmph");
  14.   }  
  15.    public static void main(String args[])
  16.   {  
  17.    Luxury luxury= new Luxury();  
  18.    luxury.run();  
  19.    }  
  20. }  

Test it Now

Output: Compile Time Error

3. Java final class

If you make any class as final, you cannot extend it.

Example of final class

  1. final class Bike
  2. {
  3.   //Some Codes
  4. }  
  5.   
  6. class Hero1 extends Bike
  7. {  
  8.   void run()
  9.  {
  10.    System.out.println("running safely with 100kmph");
  11.  }  
  12.  public static void main(String args[])
  13.  {  
  14.   Hero1 hero= new Hero1();  
  15.   hero.run();  
  16.   }  
  17. }  

Test it Now

Output: Compile Time Error


_-_-_-_-_-_-_-Doubt Questions_-_-_-_-_-

Q1) Is final method inherited?

Ans-->  Yes, final method is inherited but you cannot override it. 

For Example:

  1. class Apache
  2. {  
  3.   final void run()
  4.   {
  5.     System.out.println("running...");
  6.   }  
  7. }  
  8. class Duke extends Apache
  9.  {  
  10.     public static void main(String args[])
  11.    {  
  12.     new Duke().run();  
  13.    }  
  14. }  

Test it Now

Output: running...

Q2)  What is blank or uninitialized final variable?

Ans--> A final variable that is not initialized at the time of declaration is known as blank final variable.

If you want to create a variable that is initialized at the time of creating object and once initialized may not be changed, it is useful. For example PAN CARD number of an employee.

It can be initialized only in constructor.

Example of blank final variable

  1. class Student
  2. {  
  3.   int id;  
  4.   String name;  
  5.  final String PAN_CARD_NUMBER;  
  6.  
  7. }  

Q3) Can we initialize blank final variable?

Ans--> Yes, but only in constructor. For example:

  1. class Bike10
  2. {  
  3.   final int speedlimit;  //blank final variable  
  4.   Bike10()can 
  5.  {  
  6.   speedlimit=100;  
  7.   System.out.println(speedlimit);  
  8.  }  
  9.   
  10.   public static void main(String args[])
  11.  {  
  12.     new Bike10();  
  13.   }  
  14. }  

Test it Now

Output: 100

Q4) what is static blank final variable ?

Ans--> A static final variable that is not initialized at the time of declaration is known as static blank final variable. It can be initialized only in static block.

Example of static blank final variable

  1. class G
  2. {  
  3.   static final int data;  //static blank final variable  
  4.   static
  5.   { 
  6.     data=50;
  7.    }  
  8.   public static void main(String args[])
  9.   {  
  10.     System.out.println(G.data);  
  11.    }  
  12. }  

Q5) What is final parameter ?

Ans--> If you declare any parameter as final, you cannot change the value of it.

  1. class car
  2. {  
  3.   int cube(final int n)
  4.   {  
  5.     n=n+2;  //can't be changed as n is final  
  6.     n*n*n;  
  7.   }  
  8.   public static void main(String args[])
  9.  {  
  10.     car b=new car();
  11.     b.cube(100);
  12.  }
  13. }



Social media link: 
Click Here

THANKS FOR VISITING THIS BLOG PAGE 💖.







    Post a Comment

    0 Comments