Final Keyword in Java – Final variables, methods, and classes

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

 

1. Final variable

The value of a final variable cannot be changed 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 at the time of declaration, then it must be initialized inside constructor of the class in which it is declared. Such variable is also called a blank final variable. Any attempt to set blank final variable outside the constructor will result in a compilation error.

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

 
Consider below 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 components of the object may be changed by operations on the object, but the variable will always refer to the same object.

To illustrate that finality doesn’t guarantee immutability, consider below 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 java.lang package is final. The String class is also is final in Java.

 
To illustate the behavior of a final class, consider below code which throws an compilation error since class B is trying 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 function or consistency of the class.

 
To illustate the behavior of a final method, consider below code which throws an 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

 
References: Final (Java) – Wikipedia

 
Thanks for reading.

Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 



Leave a Reply

avatar
  Subscribe  
Notify of