userimage

What is the usage of blank final variable ?

I was asked in an interview about blank final variable ,i know about final variable but i dont have any idea about blank final variable can any one help me in this.
userimage

Please find the below points for your clarity,

  • Blank final variable in Java is a final variable which is not initialized while declaration, instead they are initialized on constructor.
  • Java compiler will complain if 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.
  • And also note is that you can not override final method and can not sub class final class, those will leads to compile time error.

And for more details go through the below example with code snippet

  • Blank final variable is like any other final variable but must be initialized in constructor as shown in below example. If you don't initialize or forget to initialize blank final variable then compiler will complain about it by throwing compile time error. static and final variable are treated as compile time constant and there value is replaced during compile time only.


public class BlankFinalVariable {

    private final int blankFinalVariable;
//must be initialized in 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



Answer is