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 change be use
1. variable in Java
2. method in Java
3. class in Java.
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
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 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
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
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
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
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
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
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
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…