Why Do We Use Final Variables In Java?

Here in this tutorial, we will learn Why Do We Use Final Variables In Java? As we know their situations when we require a value of a variable as it is though out the program so in such cases we use a final keyword before a variable to make the variable final which makes the value of the variable fixed throughout the program.

What Are the Variables?

Variables are the box or places where the values are stored for every program or project and we use variables for making certain decisions in a program to get proper outputs.

there are conditions in certain programs and projects where few things and the value of few things are fixed for the project. And at times there are situations where there are probabilities of getting manipulating the values to avoid such situations we the keyword final before the variable.

Final Variable

Final VariablesFinal variables are those variables where the values are fixed for that program, for example, the value of pie always remains constant so we can use use the keyword final before pie variable so that the variable value remains always fixed and we can not update the value of pie in that case.


let’s Take an example to understand better

final float pie =3.14;
float r = 6.99;
foalt area = pie*r*r
r = 45;

In the above question we have seen how we have taken pie as the final and tried to find the area of the circle where we could get the answer and for the next line we updated the value of r which we could as r is a simple float value but we can not update the value of pie as it was initialized as final.

So above program will give an example but if we remove the last line it will work fine or we remove the keyword final from the pie then also we can update the value and it will un without any error.

Learn More about Final keywords and their limitations here Final variable in java

And also visit JAVA TUTORIAL for all types of explanationsfor learning more about java and many more topic.


Leave a Comment

%d bloggers like this: