• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Test Case Examples
  • Interview Questions
  • Interview Questions Asked
  • Java
  • Java Program
  • Selenium
  • Selenium Programs
  • Manual Testing
  • Difference
  • Tools
  • SQL
  • Contact Us
  • Search
SoftwareTestingo » Java » Java Tutorial » Final Keyword In java

Final Keyword In java

Last Updated on: August 15, 2020 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • Final Variable
  • Initialized By Assign Statement
  • Inside Constructor Block ( Blank final variable )
  • Static Initializer Block ( Static Final Variable )
  • When to use a Final Variable
  • Final Method
  • Final Class
  • Final Keyword Important Points
  • Write a Program to Find Out About Final Variable With Example?

Final Keyword Variable, Class & Method In java: In this tutorial, we are going to learn about the Final keyword of Java programming language. Java final is a reserved or keyword which we can use with variables, methods, and classes, etc.

We are using the final keyword to make the entity as non-modifiable. That means if we declare a method or class or variable as final, then you are not allowed to change that variable or class or method. If you are trying to re-initialized a final variable, then the compiler will give you compiler error.

In Java programming language, we are using a final keyword to give restrictions to the users. We can use the Java keyword in many contexts like:

  • Final Variable
  • Final Methods
  • Final Class

Let’s discuss each of them one by one in details:

Final Variable

When you declare a variable with the final keyword, then that variable is called the final variable. The value of a final variable you cannot change once it is initialized. But the final variable is different from constant variable because it’s not compulsory to assign the value to a final variable at the time of declaration.

We can assign the variable in 3 different ways:

  • By an assign statement
  • Inside Constructor Block
  • Static Initializer Block
Final Keyword In java With Explanation
Final Keyword In java With Explanation

Initialized By Assign Statement

We can initialize a final variable at the time of the declaration by using the assignment operator. you can check the below example for understanding:

class Point
{
   public final int x = 1; // final variable
   public static void main (String[] args)
   {
      System.out.println("The Value of X is:-"+ x);
   }
}

Inside Constructor Block ( Blank final variable )

If a final variable is not initialized at the time of declaration that type of variable is called the final blank variable, then we can initialize that final variable inside the constructor of the class on which the final variable is mentioned. If you try to initialize a final blank variable in any other way, then you will get a compilation error.

Take a look at the below example of how to initialize a final blank variable:

class Demo
{
   //Blank final variable
   final int MAX_VALUE;

   Demo()
   {
      //It must be initialized in constructor
      MAX_VALUE=100;
   }
   void myMethod()
   {
      System.out.println(MAX_VALUE);
   }
   public static void main(String args[])
   {
      Demo obj=new Demo();
      obj.myMethod();
   }
}

Note: If you have more constructors in a class, then you need to be initialized in all of the constructors; otherwise, you will get a compile-time error.

Static Initializer Block ( Static Final Variable )

If a variable is declared with both static and final keyword, then such type of variable is called the final static variable. We can initialize a final static variable at the time of declaration or else inside the static block of the class in which it is declared. If a final static variable is not initialized at the time of declaration, then such variables called blank static final variables.

If you try to initialize a final static variable outside of a static block, then you will get a compilation error.

When to use a Final Variable

The difference between a normal and a final variable is we can change or re-assign the value of a normal variable in between the program. Still, if we initialize a value for a final variable, then you can’t change the value of the final variable. So it’s better to use the final variable for the constant variables of the program.

Final Method

Sometimes we want to restrict other classes to override the method of the parent classes. For achieving this, we can define the methods with the final keyword, and such methods are called final methods.

The main reason behind the final method declaration is the content of the method should not be changed by outside.

class A
{
   final void m1()
   {
      System.out.println("This is a final method.");
   }
}
class B extends A
{
   void m1()
   {
      // COMPILE-ERROR! Can't override.
      System.out.println("Illegal!");
   }
}

Final Class

If a class is defined with the keyword final, then that class is called the final class. If a class is declared as final, then it can not be extended (inherited), and final classes are completed in nature. We are using the final class for secure and efficient code. In Java programming language, several classes are already declared as final in the standard library. For example, Java.lang package and String class.

Final Keyword Important Points

  • You Can not declare constructor as final
  • If you want no other class able to extend your class then that time you can declare your class as final
  • If you have declared data members as final, then that are acts as constant. You can not change the values of those data members.
  • By default, all the variables or data members of an interface are static and final.

Write a Program to Find Out About Final Variable With Example?

package com.java.Softwaretestingblog;
public class FinalVar {
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      final int i = 10;
      //i = 30; // Error because i is final.
      System.out.println("The Value Of I Is: -"+i);
   }
}

Output:

The Value Of I Is: -10

Ref: article

    Filed Under: Java Tutorial

    Reader Interactions

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Copyright © 2023 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers