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:
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.
// 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);
}
}
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);
}
}
i=90;
^
1 error
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
{
// 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
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
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…