Thursday, 26 January 2012

Blank Final Variables in Java

The blank final, which was introduced in Java 1.1, is a final variable whose declaration lacks an initialization. A blank final must be initialized at run-time and that value remains throughout the life-time of variable.Both the static and non static variables can be declared as blank final variable.

static blank final variable


All the static blank final variables should be initialized within a static block.Example given below

class StaticBlankFianl{
final static int X;
static{
X=10;
}
public static void main(String args[]){
X=20;//Wrong !! .Once assigned we cant change value of a static blank variable
}
}
Here when the class loads ,the static block will be executed and it initializes the blank final X.

non-static blank final variable


Here all the declared blank final variables  should be initialized within at-least one constructor and within all other overloaded constructors..See the example


class BlankFinal{
final int X;
BlankFinal(){
X=10;
}
BlankFinal(int x){
X=x;
}

public static void main(String args[]){
BlankFinal f1=new BlankFinal(); //X gets value 10
BlankFinal f2=new BlankFinal(20); //X gets value 20
f1.X=20; //Wrong !! .Once assigned we cant change value of a blank variable
f2.X=30; //Wrong !! .Once assigned we cant change value of a blank variable
}
}

The following class declarations are wrong


class BlankFinal{
final int X;
BlankFinal(){  //Compilation Error !! ,X not initialized
}
BlankFinal(int x){
X=x;
}

}
Here the first constructor does't initialize the blank final X,hence gets  compilation error ,The blank final field X may not have been initialized.



class BlankFinal{
final int X;
public static void main(String args[]){
X=10;
}
}
Here also you will get the same compilation error that described above.

Summary
  • static blank final variable should be initialized within at-least and at-most one static block.
  • non-static blank final variable should be initialized within at-least one constructor and within all other overloaded constructors.
  • Once assigned the value of a blank final can't be changed throughout its lifetime.

The links that may help you.


Compile-time Constants in Java

5 comments:

  1. helpful notes..

    ReplyDelete
  2. Indeed good post. final is a tricky keyword if used carefully it can boost performance of Java application. I have also shared my view as final keyword in Java let me know how do you find it.

    ReplyDelete
  3. too good..
    thank you

    ReplyDelete
  4. Non-static blank final variable can also be initialized within an object initializer (a non-static block). The following will compile.

    class BlankFinal{
    final int X;
    {
    X=10;
    }
    BlankFinal(){...}
    BlankFinal(int x){...}
    }

    ReplyDelete
  5. nice write up. want to add that final reference variables can't be changed to point to other objects but the object itself can be modified. But real issue has been faced when we start writing our unit tests in TDD

    ReplyDelete