09.12. Final Keyword

  • Eclipse: Oxygen
  • Java: 1.8

Final Keyword

Final keyword can be applied to variables, methods, classes, etc. It is used in various contexts to define an entity that can only be assigned once. Once a final variable has been assigned, the variable cannot be changed. Therefore, the user tries to change the value using the setter or the constructor, and then the errors are entered into the program.

Final variable

If you make a variable as final, you cannot change the value of the final variable. There is a final variable x, we are going to change the value of this variable, but it cannot be changed because we have defined the final variable in constructor B().

If you make a variable as final, you cannot change the value of the final variable. In the following program, there is a final variable x.

A variable that is not initialized at the time of declaration is known as a final blank variable. We must initialize the final variable in the constructor of the class, otherwise, it will throw a compilation error.

Program folder structure

Program code:

A.java

App.java

B.java

Output

12

Contributed by Salim Sheikh

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments