Final Keyword

  • Eclipse: Oxygen
  • Java: 1.8

In this tutorial, the use of the final keyword in a program is shown. When a final keyword is assigned to a variable then the variable cannot be changed. Hence the user tries to change the value using setter or constructor then errors creep into the program.

It 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.

Output

12

App.Java

Output

12

B.Java

Output

12

More use of this word will be seen further.

Final Keyword with method and class: The final keyword is very useful for the following purposes. When we define a method as final, it can not be modified by any other class that invalidates the same method.

If a class is defined final then it cannot be used for inheritance and cannot override any method.

Output

Value of inner class variable: 0
Testing inner class method.

Output

Hello India, what’s up?
USA is fantastic

Output

Hello India, what’s up?
USA is fantastic

Contributed by: Poonam Tomar


VIEW COURSES

Buy me a coffee!

 

PayTM QR (INDIA)

  • Get video tutorials
  • Project files set
  • Get Certified

Leave a Reply

avatar
  Subscribe  
Notify of
Close Menu