What is blank final variable with Example?

What is blank final variable with Example?


1. What is blank final variable or uninitialized final variable?

A final variable declared but not assigned is known as a blank final variable or uninitialized final variable. Introduced with JDK 1.1.

2. How to initialize or give a value for an uninitialized final variable?

It is possible with a constructor call only. That is, the uninitialized final variable can be given a value through a constructor only (through method is not possible).

3. What is constructor blank variable?

A blank final variable given a value in the constructor is known as constructor blank variable.

4. Explain through an example?

Following is the example program to initialize a final blank variable.

5. If a blank final variable is not given a value in the constructor what happens?

It raises compilation error because a final blank variable should be given a value somewhere in the program and that too from a constructor only. In the above program, if "salary = 9999.99;" is commented out, compiler gives the following error (observe the screenshot).

ima

6. Does the following code compiles?

In the above code, Employee constructor is overloaded. If overloaded, in each constructor salary must be initialized as follows.

Or, atleast, a constructor can be called from another to get compiled.

7. What are the advantages of blank final variables?

Blank final variables allow the Programmer to make immutable data types. That is, final blank variables allow the class to declare immutable fields which are initialized at runtime by passing arguments to a constructor. Once assigned, the value cannot be changed accidentally.

2 thoughts on “What is blank final variable with Example?

Leave a Reply

Your email address will not be published. Required fields are marked *