Final Keyword

1. Is a keyword.

2. The final keyword can be used in many contexts. final can be: Variable Method Class

3. If you want to declare constant variables where in the value cannot change, then those variables should be declared with the keyword “final”.

4. Final variable value cannot be changed or overridden.

5. Both local and global variable can be final.

6. Global final variable should be initialized at the time of initialization itself.

7. Local final variable can be declared once and initialized later.

8. In terms of inheritance, final keyword us used to avoid overriding.

9. Final class cannot be inherited.

10. Final class methods cannot be overridden because for method overriding inheritance is mandatory

Note:  If you make a variable as final then we can never re-initialize that variable.  Static and non-static variable if made Final then its initialization is mandatory or else we will get blank field error.

Note: In case of local variable make it final there is no error, but using it without initialization will throw error.

Note: If you make an array as final then its size cannot be altered.

Last updated