We have seen what a variable is and we are also aware of the fact that a variable may be assigned a new value any number of times. That is the reason it is known as a variable i.e. something whose values keeps varying and does remains constant. Suppose that we need to use the value of PI repeatedly in our program correct to ten decimal places. Mentioning this value each time may not be a viable option. One thing that can be done is to initialise a variable named pi to its value in the following way in the beginning of the program and use the variable wherever the variable is required.
double pi = 3.1428571428;
double volume = pi * radius * radius * height;
But such a representation falsely implies that pi is a variable, something contradicting the fact that everyone is aware of. Moreover, one might erroneously change the value of the variable pi to a new value.
pi = 4.1458;
To avoid such mishaps, we have another type of variables in Java known as final variables. The value of such variables once initialised may not be changed later on during the execution of the program. Final variables are declared using the final modifier in the following way:
final double pi = 3.1428571428;
If we later on try to modify the value, as we have done earlier, compilation errors occur. So, marking a variable whose value shouldn't be changed helps it from being modified accidentally. This value of pi can be used just like any other value. In order to distinguish a final variable from a non final variable, we name it in all uppercase letters. If the variable name consist of more than a single word, they are separated by an underscore ( _ ) sign. This is the naming convention that everyone follows. For example:
double PI = 3.1428571428;
final int NUMBER_OF_TURNS = 34;
Final variables may be declared as either static or non static. However, it doesn't make any difference since the value of that variable can however not be changed. The only reason that could be cited for declaring a final variable as non static is to restrict its access only to objects of that class and avoid it from being accessed without creating its object.
Also, the value of a final variable should be initialised at the time of its declaration itself. For example, the statements below would result in errors:
final int A_STATIC_VARAIBLE ;
A_STATIC_VARAIBLE = 34;
The reason behind this is that all uninitialized variables are initialized by the compiler with their default values when creating objects of that class type. When the initialisation takes place to the programmer defined value, the task is equivalent to reassigning the variable with a new value.
Use of final variables also betters reflects the task being done and makes the program self explanatory. For example, look at the following statement:
double average = (double)( marks1 + marks2 + marks3 ) / 3;
At the first look the number 3 might not appear to be the total number of subjects. But if we use the following sets of statements, the expression becomes self explanatory.
int NUMBER_OF_SUBJECTS = 3;
double average = (double)( marks1 + marks2 + marks3 ) / NUMBER_OF_SUBJECTS;
Just like final variables, we also have final methods and final classes. The use of final with these entities also means the same as seen with variables. We shall look into it when we deal with inheritance.