What is blank final variable in Java
Blank final variable in Java is a final variable which is not initialized while declaration, instead they are initialized in a constructor. Java compiler will complain if a blank final variable is not initialized during construction. If you have more than one constructor or overloaded constructor in your class then blank final variable must be initialized in all of them, failing to do so is a compile time error in Java. Alternatively, you can use constructor chaining to call one constructor from other using this keyword, in order to delegate initialization of blank final variable in Java. In this Java tutorial, we will see What is blank final variable in Java and a code example on How to use a blank final variable.
How to use blank final variable in Java with Example

Java Program to use a blank final variable
/**
*
* Java program to demonstrate that blank final variable in Java
*
* Java program to demonstrate that blank final variable in Java
* must be initialized in the constructor.
* Failing to initialized blank final variable will result in compile time error.
* @author http://java67.blogspot.com
*/
public class BlankFinalVariable {
private final int blankFinalVariable; //must be initialized in a constructor
public BlankFinalVariable(){
this(6); // this is Ok
//this.blankFinalVariable = 3;
}
public BlankFinalVariable(int number){
this.blankFinalVariable = number;
}
public static void main(String args[]) {
BlankFinalVariable clazz = new BlankFinalVariable();
System.err.println("Value of blank final variable in Java : " + clazz.blankFinalVariable);
}
}
Output:
Value of blank final variable in Java : 6
Output:
Value of blank final variable in Java : 6
* Failing to initialized blank final variable will result in compile time error.
* @author http://java67.blogspot.com
*/
public class BlankFinalVariable {
private final int blankFinalVariable; //must be initialized in a constructor
public BlankFinalVariable(){
this(6); // this is Ok
//this.blankFinalVariable = 3;
}
public BlankFinalVariable(int number){
this.blankFinalVariable = number;
}
public static void main(String args[]) {
BlankFinalVariable clazz = new BlankFinalVariable();
System.err.println("Value of blank final variable in Java : " + clazz.blankFinalVariable);
}
}
Output:
Value of blank final variable in Java : 6
Output:
Value of blank final variable in Java : 6
That's all on What is blank final variable in Java and how to use them. If you wan to learn more about What is final variable or final class in Java, then see that link. Another important point to note is that you can not override final method and can not sub class final class, those will result in compile time error.
Further Learning
Data Structures and Algorithms: Deep Dive Using Java
Java Fundamentals: The Java Language
Complete Java Masterclass
>
Other Java fundamental tutorial you may like
When do some one should use blank final variable? Do you have a genuine use case for blank final variables in Java ?
ReplyDeleteI think, if you are initializing final variable at the time of object creation, and especially by value passed to constructor, than use blank final variable. For example, you can pass date of birth while creating a Person class, if this would be stored in blank final variable, you can not change it during object's life time.
Deletecan we initialize this without constructor like in other methods...???
ReplyDeleteNo u cnt initialize this without constructor
DeleteYes..we can using Instance initialisation block.
Delete