Final is a keyword in Java that can be applied to variables, methods, and classes to restrict their behavior. This post provides an overview of the behavior of variables, methods, and classes in Java when a final keyword is applied. Let’s discuss each one of them in detail:

1. Final variable

We cannot change the value of a final variable once it is initialized. A final variable is different from a constant as the value of a final variable is not necessarily known at compile time.

A final variable can only be initialized once, either via an initializer or an assignment statement. If a final variable is not initialized during declaration, it must be initialized inside the class’s constructor in which it is declared. Such a variable is also called a blank final variable. Any attempt to set a blank final variable outside the constructor will result in a compilation error.

Similarly, if a static final variable is not initialized at the declaration, then it must be initialized inside the static initializer block of the class in which it is declared. Such a variable is called a blank static final variable. Any attempt to set a blank static final variable outside the static initializer will result in a compilation error.

 
Consider the following code. Here any attempt to reassign x, y, or z will result in a compilation error.

Download  Run Code

Output:

(1,2,3)

 
If a final variable holds a reference to an object, then the object’s components may be changed by operations on the object. Still, the variable will always refer to the same object.

To illustrate that finality doesn’t guarantee immutability, consider the following code, where we have replaced the x, y, z variables with a single Position object with three properties x, y, and z. Now Position object cannot be assigned to, but the three properties can unless they are themselves final.

Download  Run Code

Output:

(1,2,3)
(10,20,30)

2. Final class

If a class is declared as final in Java, then it cannot be extended. A final class is used for secure and efficient code. Several classes in the Java standard library are final. For instance, the System class in the java.lang package is final. The String class is also is final in Java.

 
To illustrate a final class’s behavior, consider the following code, which throws a compilation error since class B tries to extend a final class A.

Download  Run Code

Output (Compilation error):

Main.java:3: error: cannot inherit from final A
class B extends A { // error!
^

3. Final method

If a method is declared as final in Java, then it cannot be overridden by any subclass of the class in which it is declared. This is used to prevent unexpected behavior from a subclass altering a method that may be crucial to the class’s method or consistency.

 
To illustrate the behavior of a final method, consider the following code, which throws a compilation error:

Download  Run Code

Output (Compilation error):

Main.java:8: error: fun() in B cannot override fun() in A
    public void fun() {
                 ^
  overridden method is final

That’s all about the final keyword in Java.

 
Reference: Final (Java) – Wikipedia