Tech Study

Java Final keyword

Introduction :

java final keyword

The final keyword present in Java programming language is generally used for restricting the user. This final keyword can be used in many context:

Variable

Method

Class

This keyword can be applied with the variables, which means the final variable which doesn’t have any value is called a blank variable or uninitialized final variable. The variable can be initialised in the  constructor itself.

Java final Variable:

 Once any variable is taken to be final then the value of that variable cannot be changed.

For Example:

Considering that there is some final variable named ri_speedlimit, we will try to change the value of this variable, but we will not be able to do so as we know that we are not allowed to change  the value of the final variable.

class ri_Bike9{  

 final int ri_speedlimit=100;//This is final variable  

 void ri_run(){  

  ri_speedlimit=200;  

 }  

 public static void main(String args[]){  

 ri_Bike9 robj=new  ri_Bike9();  

 robj.ri_run();  

 }  

}//ending of the class

Output: Compile Time Error

Java Final Method:

Once a method is taken to be final it cannot be overridden.

Example:

class ri_Bike{  

  final void run(){System.out.println("It is running");}  

}  

     

class ri_Honda extends ri_Bike{  

   void ri_run(){System.out.println(" Bike is running with 100kmph");}  

     

   public static void main(String args[]){  

   ri_Honda rhonda= new ri_Honda();  

   rhonda.ri_run();  

   }  

}

Output:Compile Time Error

Java Final Class:

Once the class is taken to be final it cannot be extended.

Example:



final class ri_Bike{}  

  

class ri_Honda1 extends ri_Bike{  

  void ri_run(){System.out.println("Bike is running with 100kmph");}  

    

  public static void main(String args[]){  

  ri_Honda1 rhonda= new ri_Honda1();  

  rhonda.ri_run();  

  }  

}

Output:Compile Time Error

Blank or uninitialized final variable:

Any final variable that doesn’t get initialised at the time of declaration can be called as blank final variable.

If we decide to create any variable that gets initialised at the time of creating object and once it gets initialised it cannot be changed, it is useful. For example PAN Card number .

Example :

class ri_Student{  

int ri_id;  

String ri_name;  

final String ri_PAN_CARD_NUMBER;  

...  

}

Static blank final variable:

A static final variable that does not gets initialised during the time of the declaration can be called as static blank final variable. It is supposed to be initialized only in the static block.

Example:

class ri_A{  

  static final int ri_data;//This is our static blank final variable  

  static{ ri_data=30;}  

  public static void main(String args[]){  

    System.out.println(ri_A.ri_data);  

 }  

}

java final keyword

Final Parameter:

If any parameter is declared to be final then its value cannot be changed.

class ri_Bike11{  

  int ri_cube(final int rn){  

   rn=rn+2;//value can't be changed as rn here is a  final  

   rn*rn*rn;  

  }  

  public static void main(String args[]){  

    ri_Bike11rb=new ri_Bike11();  

    rb.ri_cube(5);  

 }  

}

Java Final Keyword

Output: Compile Time Error

 

C++ Memory Management: new and delete

C++ Memory Management We know that arrays store contiguous and the same type of memory blocks, so memory is allocated …

Read more

If-else statement in java

Introduction: If-else statement in java Java if statement can generally used for checking the condition. It is supposed to check …

Read more