Java Made Simple: What is Java final variable with Example?

Java Made Simple: What is Java final variable with Example?

In Java programming, Java final keyword attains good importance as it can be applied in three places for three different entities – variables, methods and classes. In each place works differently and gives three different functionalities (outputs).
  1. A Java final variable cannot be reassigned.
  2. A Java final method cannot be overridden.
  3. A Java final class cannot be inherited.

In this tutorial, we are interested in the first entity – Java final variables.

Java does not support "const" (constant) keyword of C/C++. "const" is replaced with final in Java. A final variable cannot be reassigned. That is, once a value for a final variable is assigned, it cannot be changed in the remaining code.

Let us write a small code and explain and then further dig in the concept.

By common sense, marks of a student can be changed but not his age. For this reason, age is declared as final.

Restrictions on the usage of final variables

Is it necessary to initialize a final variable or a final variable can be declared first and then assigned? Following examples give the answer.

1. Does the following code works?

The above code works fine and prints 80. A local final variable can be declared and assigned later. The reason is local variable, if not assigned, does not take a default value immediately. So, the local final variable can be given a value later after declaring it. But it is not possible with instance variable. See the next question.

2. Does the following code works?

 Java final

Observe the above screenshot of compilation error. marks is instance variable not given a value at the time of declaration. In Java, if instance variables are not given a value, they take default values. For whole numbers, it is 0 value. So now, marks is given 0 value but here important is marks is declared final. When marks gets 0 value, it cannot be changed in the subsequent code.

3. Does the following code works?

In the above code, value for marks is assigned through a method call display() and is not working. But it is possible to assign through a constructor. See this code.

In the above code, final marks is first declared and then assigned a value through constructor. It is working. As programmer, it is required to be aware of all these points.

We can derive two important rules from the above three questions.

  1. final local variables can be declared and then assigned (in two different statements).
  2. final instance variables should be assigned a value
    1. at the time of declaration itself
    2. through a constructor call

But not possible through a method call.

3. In the following code which is final – object marks array or values of marks elements like marks[0]?

Here, final is object of marks which cannot be reassigned. But not values. It is shown in the above code.

Following is the screenshot when the comment for "// marks = new int[1];" is removed.

 Java final

Storing a reference to object marks is final. That is, marks becomes immutable.

Some salient points to be noted still with final variables in a nutshell
(these points are important especially in interviews)

  1. final is the replacement of C/C++ "cosnt" keyword.
  2. "final" variable can be applied in three different contexts – variables, methods and classes.
  3. Common meaning of final is that something cannot be done.
  4. final variable can be assigned only once (cannot be reassigned).
  5. A blank variable forces the constructor to initialize with a value (but not possible with method).
  6. final, finally and finalize are very different.
  7. The variables declared within an interface are implicitly final.
  8. final serves code to be more clear (reader comes to a conclusion that the value never changes and is the same throughout the program) and helps in self-documenting the code. Code maintenance will be easier.
  9. JRE/JIT can make optimizations with final variables to improve performance.
  10. By C/C++ convention, Java Programmer writes final variables in uppercase (like predefined FlowLayout.LEFT, Thread.MIN_PRIORITY, Font.BOLD etc.).
  11. final is often used with static keyword for much more performance and easy in coding to call with class name(like predefined FlowLayout.LEFT, Thread.MIN_PRIORITY, Font.BOLD etc.).
  12. Examples where variable are to be declared final: PAN card number, identification marks, Adhar card number, Bank account number etc.
  13. In a programming language, instance variables (known as fields) denote the state of the object. If is safer to use final variables where Programmer need not think about the object state.

================Know more deep of final variables================

1. What is blank final variable with Example?
2. What is static blank final variable in Java with Example?

Leave a Reply

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