What is blank final variable? What are Static blank final variables in Java?

Java 8Object Oriented ProgrammingProgramming

Static variables − Static variables are also known as class variables. You can declare a variable static using the keyword. Once you declare a variable static there would only be one copy of it in the class, regardless of how many objects are created from it.

public static int num = 39;

Instance variables − These variables belong to the instances (objects) of a class. These are declared within a class but outside methods. These are initialized when the class is instantiated. They can be accessed from any method, constructor or blocks of that particular class.

You must access instance variables using an object. i.e. to access an instance variable you need to create an object of the class and using this object you need to access these variables.

final − Once you declare a variable final you cannot reassign value to it.

Blank variables

A final variable which is left without initialization is known as blank final variable. Like instance variables, final variables will not be initialized with default values. Therefore, it is mandatory to initialize final variables once you declare them.

Still, if you try to use blank variables in your code, a compile time error will be generated.

Example

In the following Java program, the class Student contains two final variables name and age and they have not been initialized.

public class Student {
   public final String name;
   public final int age;
   public void display(){
      System.out.println("Name of the Student: "+this.name);
      System.out.println("Age of the Student: "+this.age);
   }
   public static void main(String args[]) {
      new Student().display();
   }
}

Compile time error

On compiling, this program generates the following error.

Student.java:3: error: variable name not initialized in the default constructor
   private final String name;
                        ^
Student.java:4: error: variable age not initialized in the default constructor
   private final int age;
                     ^
2 errors

Solution

To resolve this, you need to initialize the declared final variables as −

public class Student {
   public final String name;
   public final int age;
   public Student(){
      this.name = "Raju";
      this.age = 20;
   }
   public void display(){
      System.out.println("Name of the Student: "+this.name );
      System.out.println("Age of the Student: "+this.age );
   }
   public static void main(String args[]) {
      new Student().display();
   }
}

Output

Name of the Student: Raju
Age of the Student: 20

Static blank final variable

In the same way if you declare a static variable final without initializing it, it is considered as static final variable.

When a variable is declared both static and final you can initialize it only in a static block, if you try to initialize it elsewhere, the compiler assumes that you are trying to reassign value to it and generates a compile time error −

Example

class Data{
   static final int num;
   Data(int i){
      num = i;
   }
}
public class ConstantsExample {
   public static void main(String args[]) {
      System.out.println("value of the constant: "+Data.num);
   }
}

Compile time error

ConstantsExample.java:4: error: cannot assign a value to final variable num
   num = i;
   ^
1 error

Example

Therefore, it’s a must to initialize a static final variable in a static block.

To make the above program work you need to initialize the final static variable in a static block as −

class Data{
   static final int num;
   static{
      num = 1000;
   }
}
public class ConstantsExample {
   public static void main(String args[]) {
      System.out.println("value of the constant: "+Data.num);
   }
}

Output

value of the constant: 1000
raja
Published on 05-Aug-2019 17:56:25
Advertisements