Categories: Java

Final keyword in Java

In Java, the final keyword is used to restrict the user. The final keyword in Java can be used in a variety of situations. Finally, you could say:

  • variable
  • method
  • class


The final keyword can be used with variables to create a final variable with no value. It’s also known as an uninitialized final variable or a blank final variable. It can only be set up in the constructor. The static final variable can alternatively be blank, and it will only be initialized in the static block. These will be thoroughly studied by us.

Final Variables:
  • When a variable is declared using the final keyword, the value of the variable cannot be changed, making it a constant.
  • This also necessitates the creation of a final variable.
  • If the final variable is a reference, it cannot be re-bound to reference another object, but the internal state of the object indicated by that reference variable can be altered, allowing you to add or delete elements from the final array or collection.
  • It’s best to write final variables in all capital letters, with underscores between terms.
Example:
// Java program to demonstrate
// local final variable

// Main class
class GFG {

 // Main driver method
 public static void main(String args[])
 {
  // Declaring local final variable
  final int i;

  // Now initializing it with integer value
  i = 20;
       
  // Printing the value on console
  System.out.println(i);
 }
}
Output:
20

If we run the above code but after altering the value of variable i, then we get an error:

// Java program to demonstrate
// local final variable

// Main class
class GFG {

 // Main driver method
 public static void main(String args[])
 {
  // Declaring local final variable
  final int i;

  // Now initializing it with integer value
  i = 20;
        i=90
  // Printing the value on console
  System.out.println(i);
 }
}
Output:
i=90;
^
1 error
Final Methods:
  • A method is called a final method when it is declared with the final keyword.
  • It is not possible to override a final method.
  • This is done by the Object class, which has a number of final methods.
  • We must use the final keyword to declare methods for which we must use the same implementation throughout all derived classes.
Example:
class A 
{
    final void m1() 
    {
        System.out.println("This is a final method.");
    }
}

class B extends A 
{
    void m1()
    { 
        // Compile-error! We can not override
        System.out.println("Illegal!");
    }
}
Final Class:
  • A final class is one that has been declared with the final keyword. It is not possible to extend a final class (inherited).
  • A final class can be used in two ways:
  • The first is to avoid inheritance because final classes cannot be extended. All Wrapper Classes, such as Integer, Float, and others, are final classes. We cannot extend them.
  • Another way to utilize final with classes is to make an immutable class, such as the String class. It is impossible to make a class immutable without also making it final.
Example:
final class A
{
     // methods and fields
}
// The following class is illegal
class B extends A 
{ 
    // COMPILE-ERROR! Can't subclass A
}

Note: also read about the Static keyword in Java

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago