What is static blank final variable in Java with Example?


We have seen earlier, blank final variable and let us see now static blank final variable.

1. What is blank final variable?

A final variable declared but not initialized (or not given a value) is known as blank final variable. It can be given a value through a constructor only (but not through a method call).

2. What is static blank final variable?

It is simply a blank final variable declared extra as static. That is, a final static variable declared but not given a value or not initialized is known as static blank final variable.

3. How to give a value to a static blank final variable?

It can be given value (or initialized) through a static block only.

4. Give an example on the initialization of static blank final variable?

Following example illustrates.

public class Employee
{
  final static double salary;

  static				          // static block
  {
    salary = 9999.99;
  }
  public static void main(String args[])
  {
    System.out.println(Employee.salary);          // prints 9999.99
  }
}

In the above code, "salary = 9999.99;" is commented in the static block, it raises compilation error. It is because a static blank final variable should be initialized somewhere in the program and that too from a static block only.

5. What is the usage of static blank final variable?

It allows a blank final variable to get initialized (through static block) at the time of execution before any method or constructor called. It works like a constant and increases performance with JVM optimizations.

6. What is the difference between blank final variable and static blank final variable?

The difference is just static keyword. A simple blank final variable can be initialized through a constructor where as static blank final variable through a static block only.

4 thoughts on “What is static blank final variable in Java with Example?”

    1. One of the functionalities of a constructor is Developer can use the constructor to assign properties (values to instance variables) to an object at the time of creation itself. If an object is created without assigning values, the object takes default values to variables. Once taken, as it is final variable, a value cannot be given. This is the reason.

Leave a Comment

Your email address will not be published.