Usage of final in Java programming language
- Home
- Keyword in Java
- Usage of final in Java programming language
- On
- By
- 0 Comment
- Categories: Keyword in Java
Usage of final in Java programming language
usage of final in Java programming language
We will discuss in this tutorial about the usage of final in Java programming language
final is a java keyword, it is used to restrict the user, When the variable is defined as final, you can not change the value of the final variable. So its constant The final keyword can use 3 situations
Final keyword
final change be use
1. variable in Java
2. method in Java
3. class in Java.
- When the final variable is defined, you can not changed the value of the final variable so it is constant.
final variable
The final keyword can be applied with the variable.
final int MY_AGE=23; //this value can not be changed
a final variable that has no value it is called blank final variable
blank final variable
A fanal variable that has no value it is called blank final variable
final int MY_MARKS; // this is a uninitialized variable called final variable
2. When the final method is declared, you can not override it
final method
final void move() { System.out.print("this is a final method") System.out.print("you can not override me") }
3. When the final class is declared, you can not inherit or extend it
final class
Syntax
final class Override{ System.out.print("I am a final class"); } class Not_override extends Override{ void override1() { System.out.print("you can not extend me"); } }
Example
final variable declaration program
Program 1
class final_keyword{ final int stu_marks=90; //final variable can not be changed void marks1(){ stu_marks=56;//re-assign value for variable System.out.println("marks is"+stu_marks); }//display value for stu_marks public static void main (String args[]){ final_keyword obj=new final_keyword(); obj.marks1(); } }
When the above code is compiled and executed, it will produce the following results
Compile time error
Because cannot be changed from “into stu_marks=90”, if it is declared as final.
program 2
blank final variable declaration program
class StudentMarks{ final int Stu_Marks; //Blank final variable StudentMarks(int marks){ //this is a constructor Stu_Marks=marks; //variable initialized inside the constructor } void my_Marks(){//this is a method System.out.println("student marks :"+Stu_Marks); } public static void main(String args[]){ StudentMarks stu=new StudentMarks(57); //create object and pass argument stu.my_Marks();//called method } }
When the above code is compiled and executed, it will produce the following results
student marks :57
Program 3
Final method declaration program
When the final method is declared, you can not override it but it is inherited.
class final_method{ final void demo_Final(){//method is declared as final System.out.println("I am a final method"); } } class normal_Method extends final_method{ void demo_Final(){//Normal method System.out.println("I am a Normal method"); } public static void main (String args[]){ normal_Method obj=new normal_Method(); obj.demo_Final(); } }
When the above code is compiled and executed, it will produce the following results
Compile time error
At this program base class have a final method but derived class have a normal method. When we run the program Can not be overriding final method
Final parameter to pass method
When we declare a method , we can pass a parameter as final, but we cannot change the value of it.
class Area_Square{ int area(final int x){//parameter as final x=x+3;//cannot be change valued: x*x; } Public static void main(String args[]){ Area_Square obj=new Area_Square(); obj.area(4); } }
When the above code is compiled and executed, it will produce the following results
Compile time error
Final class declaration program
When we make any class as final we cannot extend it
Program 4
final class Base_Class{ } class Derived_Class extends Base_Class{ void demo_method(){ System.out.println("this is my demo"); } public static void main(String args[]){ Derived_Class obj=new Derived_Class(); obj.demo_method(); } }
When the above code is compiled and executed, it will produce the following results
Compile time error
Final class cannot be inherited by chiled classs
There are other Java language keywords that are similar to this keyword