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
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.
Leave a Comment