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.

Usage of final keyword
  1. 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

Output

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

 

Output

Final class cannot be inherited by chiled classs

 

There are other Java language keywords that are similar to this keyword

abstract keyword in Java

boolean Keyword in Java

break keyword in Java

Byte keyword in Java 

case keyword in Java 

catch keyword in Java 

char keyword in Java 

class keyword in Java 

continue keyword in Java 

default keyword in Java 

do keyword in Java

double keyword in Java

else keyword in Java

enum keyword in Java

extends keyword in Java

final keyword in Java

finally keyword in Java

float keyword in Java

for keyword in Java

if keyword in Java

static keyword in Java

super keyword in Java

transient keyword in Java

public keyword in Java

private keyword in Java

protected keyword in Java

 

Suggested for you

Usage of this statement in the Java language

Keywords in Java language

Keywords in C language

Keywords in Python language

One dimension Array in Java

Two dimension Array in Java

Three dimension Array in Java

Keywords in C language

Keywords in Python language

Usage of super in Java programming language
The new keyword in Java programming language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago