Final keyword in java
1. Overview
The final keyword is used to restrict the features or modifications. It can be used with the following:
- Classes
- Methods
- Variables
- Parameters
Let's discuss them one by one
2. Final classes
One of the main features of the java language is inheritance i.e. a class can extend another class to inherit its features.
But, sometimes there are situations where you don't want a class to inherited by the other class. To make sure that no other class inherits a class we use the final keyword and make that class final.
Wrapper classes are examples of final classes in java.
class final MyClass{
// variables and methods
}
Now, no other class can extend this class. If you try to extend this class you will get an error saying cannot inherit from final MyClass
Learn more about inheritance here.
3. Final methods
To stop the overriding of the methods, we make those methods final. This is useful where you need to inherit a class but you don't want a method or methods to be overwritten by any of the sub-classes. The sub-classes can use the method without overwriting it.
// This method can't be overwritten
public final void aFinalMethod() {
System.out.println("This is a final method");
}
No sub-class can override a final method but it can still be invoked from the super-class.
4. Final variables
4.1. Final non-static variables
A final non-static variable once initialized cannot be modified. It is useful where you don't want the value of a variable to be modified once it is initialized.
A final variable can be initialized
4.1.1. While declaring
class MyClass{
public final double PI = 3.14;
}
4.1.2. In the constructor
// final variable
public final int MAX_VALUE;
public FinalTest(int value) {
MAX_VALUE = value;
}
4.1.3. In anonymous block
// final variable
public final int MAX_VALUE;
{
MAX_VALUE = 548;
}
Learn more about anonymous block here.
4.2. Final static variable
A final static variable is the same as the non-static final variable. Once initialized it cannot be modified. The only thing different is that it cannot be initialized in the constructor or anonymous block.
It can only be initialized in the following ways:
4.2.1 While declaring
// final variable
public final static int MAX_VALUE = 454;
4.2.2. In static block
// final variable
public final static int MAX_VALUE;
static {
MAX_VALUE = 548;
}
Learn more about anonymous block here.
4.3. Final reference variable
A final reference variable once created cannot be reassigned to another object.
// a final reference variable
final FinalClass finalObject = new FinalClass();
The finalObject
cannot be reassigned to another object.
5. Final parameters
We can also make the parameters of a method final. It means that the values of those parameters cannot be modified inside that method.
// method with final parameters
public void add(final int num1, final int num2) {
System.out.println("Sum : " + num1 + num2);
}
Now the values of num1
and num2
cannot be modified.
6. Summary
In this article, we saw what is 'final' keyword in java and what are its uses, and how to use it to solve some problems.