Blank Final in Java

by | May 3, 2021 | Java

Home » Java » Blank Final in Java


Final Variables in Java are those group of variables that you will not want to change throughout the scope of your program. A value once assigned to a final variable cannot be changed thereafter. If you try changing it, you will get a compilation error.


final int len = 20;
len = 30;

//The second line in the code snippet will return an error since you are trying to change a final variable.

Blank Final Variables

To avoid such compilation errors as mentioned above, a final variable can be declared beforehand and assigned a value later. The empty declaration is called a blank final in java.


//Code to illustrate a blank final variable

final int len;
len = 20;

Sample Program

//The following code will demonstrate how to declare blank final variables in Java and in which cases are these variables particularly useful
class Demo
//A blank final variable will be initialised here with assigning any value
final int m;
Demo (int k)
//The blank final variable needs to be initialised here
m = k;
class Main
public static void main ( String args [] )
Demo d1 = new Demo (10);
System.out.println (d1.m);
Demo d2 = new Demo (20);
System.out.println (d2.m);



Code Explanation

As we had defined a blank final variable, it had to be initialized in the constructor of the class. The main class created objects of the Demo class and printed out the value of the final variable in two occasions. In case of an overloaded constructor, the blank final needs to be initialized in all of them.


Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.